Instantly share code, notes, and snippets.

Jing Zhang jingz8804

View GitHub Profile
View Solution1
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++;
jingz8804 /
Created Oct 21, 2014
Java Daemon Example, all credit to user Sreekesh Okky in this post
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
Thread.sleep(3000); // wait for 3 seconds. This actually let the daemon thread to print out some messages.
jingz8804 /
Created Oct 19, 2014
Use volatile keyword to prevent thread caching variables
package lecture2;
public class App{
public static void main(String[] args){
Processor prc = new Processor();
System.out.println("Press Return to stop");
jingz8804 /
Last active Sep 5, 2018
The ways to create Java threads
package lecture1;
public class App{
public static void main(String[] args){
Runner1 r1 = new Runner1();
Runner2 r2 = new Runner2();
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
jingz8804 /
Created Aug 13, 2014
#leetcode wordladder
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
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;
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
// 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;
// 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.