Skip to content

Instantly share code, notes, and snippets.

View NeoZhangTCL's full-sized avatar

Tuochaolong Zhang NeoZhangTCL

View GitHub Profile
// hello, Frank
//input: "this is a test"
//output: "test a is this"
// "hello " -> " hello"
// " hello " -> " hello "
public String reverseString(String s) {
@NeoZhangTCL
NeoZhangTCL / game2048.py
Created January 2, 2019 01:41
Game 2048 terminal version
import random
debug = False
class Game1024:
def __init__(self, t):
self.board = [[0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0]]
self.target = t
@NeoZhangTCL
NeoZhangTCL / src_Game.java
Last active May 31, 2018 19:39
a game of life
import javafx.scene.SnapshotParametersBuilder;
import javafx.util.Pair;
import java.util.*;
public class Game {
private Set<Pair<Integer, Integer>> board = new HashSet<>();
private Map<Pair<Integer, Integer>, Integer> liveNeigborMap = new HashMap<>();
/*
https://discuss.leetcode.com/topic/19853/kadane-s-algorithm-since-no-one-has-mentioned-about-this-so-far-in-case-if-interviewer-twists-the-input
*/
public int maxProfit(int[] prices) {
int maxCur = 0, maxSoFar = 0;
for(int i = 1; i < prices.length; i++) {
maxCur = Math.max(0, maxCur += prices[i] - prices[i-1]);
maxSoFar = Math.max(maxCur, maxSoFar);
}
public class Solution {
public boolean isPowerOfThree(int n) {
if (n==0) return false;
if (n==1) return true;
int sum = 0, m=n;
while (n != 0) {
sum += (n%10);
n /= 10;
}
if (sum%3==0) return isPowerOfThree(m/3);
public class Solution {
public void moveZeroes(int[] nums) {
LinkedList<Integer> zeroPos = new LinkedList<>();
if (nums.length > 0){
for(int i=0; i<nums.length; i++){
int curr = nums[i];
if (curr==0) zeroPos.add(i);
if (curr!=0 && !zeroPos.isEmpty()){
nums[zeroPos.removeFirst()] = curr;
nums[i] = 0;
int addDigits(int num) {
int res = num % 9;
return (res != 0 || num == 0) ? res : 9;
}
public class Solution {
public char findTheDifference(String s, String t) {
int alpha[] = new int[26];
Arrays.fill(alpha,0);
for (char c : s.toCharArray())
alpha[ c - 'a' ]++;
for (char c : t.toCharArray())
if (--alpha[c - 'a'] < 0) return c;
return 0;
}
public class Solution {
public String reverseString(String s) {
StringBuilder sb = new StringBuilder(s);
for(int i=0, j=s.length()-1; i<j; i++, j--){
char temp = sb.charAt(i);
sb.setCharAt(i, sb.charAt(j));
sb.setCharAt(j, temp);
}
return sb.toString();
}
public class Solution {
public int lastRemaining(int n) {
boolean dir = true; //0 for to right,
int size = n;
int move = 1;
int firstNum = 1;
while(size>1){
if( dir || (size%2==1)) firstNum+=move;
size/=2;
move*=2;