This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// hello, Frank | |
//input: "this is a test" | |
//output: "test a is this" | |
// "hello " -> " hello" | |
// " hello " -> " hello " | |
public String reverseString(String s) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int addDigits(int num) { | |
int res = num % 9; | |
return (res != 0 || num == 0) ? res : 9; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
NewerOlder