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 Set<Integer> pickDistinctNumbers(int k, int[] numbers){ | |
if(numbers == null || numbers.length == 0) return null; | |
int len = numebrs.length; | |
if(k <= 0 || k > len) return null; // ask about these situations. this is just an example | |
Set<Integer> result = new HashSet<Integer>(); // Sets.newHashSet() if you use Google Guava | |
int size = 0; | |
while(size < k){ | |
int picked = (int) (Math.random() * len); | |
if(result.add(numbers[picked])) size++; |
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 App{ | |
public static void main(String[] args){ | |
DaemonThread dt = new DaemonThread(); | |
dt.setDaemon(true); // must be set before thread starting; | |
dt.start(); // spawning the thread | |
try{ | |
Thread.sleep(3000); // wait for 3 seconds. This actually let the daemon thread to print out some messages. |
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
package lecture2; | |
public class App{ | |
public static void main(String[] args){ | |
Processor prc = new Processor(); | |
prc.start(); | |
System.out.println("Press Return to stop"); |
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
package lecture1; | |
public class App{ | |
public static void main(String[] args){ | |
Runner1 r1 = new Runner1(); | |
Runner2 r2 = new Runner2(); | |
r1.start(); | |
r2.start(); |
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 GasStation{ | |
public int canCompleteCircuit(int[] gas, int[] cost){ | |
// first check special cases | |
if(gas == null || cost == null) return -1; | |
if(gas.length != cost.length) return -1; | |
// get the number of the gas stations | |
int N = gas.length; | |
int leftAmount = 0; // the amount of gas left in the tank when we are at station i |
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 java.util.Queue; | |
import java.util.LinkedList; | |
import java.util.Hashtable; | |
import java.util.Arrays; | |
public class Solution { | |
public int ladderLength(String start, String end, Set<String> dict) { | |
if (start.equals(end)) | |
return 1; | |
// add the string end to the dict |
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 java.util.Stack; | |
public class ValidParenthesis { | |
public boolean isValid(String s) { | |
if(s == null || s.length() == 0 || s.length() % 2 == 1) return false; | |
Stack<Character> chars = new Stack<Character>(); | |
int len = s.length(); | |
for(int i = 0; i < len; i++){ | |
char c = s.charAt(i); | |
if(!validCharacter(c)) return false; |
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 java.util.Hashtable; | |
import java.util.ArrayList; | |
public class TwoSum { | |
public int[] twoSum(int[] numbers, int target) { | |
int[] result = new int[2]; | |
if(numbers == null || numbers.length <= 1) return result; | |
Hashtable<Integer, Integer> freqs = new Hashtable<Integer, Integer>(); | |
Hashtable<Integer, ArrayList<Integer>> indices = new Hashtable<Integer, ArrayList<Integer>>(); | |
// first pass, count the frequency and save the index |
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
// the recursive mergesort divides an array with length N to N singlton array | |
// and then merge them together back to one sorted array. It's a top down approach. | |
// All the work of sorting is done by the merge action. | |
// the iterative version is however, a bottom up approach. we skip the divide step | |
// and iteratively call the merge action from the beginning. | |
// however, the indexing tuning can be a bit annoying. A goog alternative is to use | |
// a queue. We dequeue two arrays from the queue, merge them together and put the merged | |
// array back to the queue. This repeats until there's only one array in the queue, the final | |
// sorted one. | |
import java.util.Queue; |
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
// we know how to do it if the path must start at the root. | |
// we can use this to solve this problem. At first glance, | |
// at each node we should look for sum path start there. | |
// actually, we could think about print out all the paths that | |
// ends there instead. | |
public class PrintPath{ | |
public void printPath(Node root, int target){ | |
if(root == null) return; | |
int depth = getDepth(root); | |
int[] path = new int[depth]; // the path is at most depth. |
NewerOlder