Skip to content

Instantly share code, notes, and snippets.

Jing Zhang jingz8804

Block or report user

Report or block jingz8804

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
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
jingz8804 / App.java
Created Oct 21, 2014
Java Daemon Example, all credit to user Sreekesh Okky in this post http://stackoverflow.com/questions/2213340/what-is-daemon-thread-in-java
View App.java
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.
@jingz8804
jingz8804 / App.java
Created Oct 19, 2014
Use volatile keyword to prevent thread caching variables
View App.java
package lecture2;
public class App{
public static void main(String[] args){
Processor prc = new Processor();
prc.start();
System.out.println("Press Return to stop");
@jingz8804
jingz8804 / App.java
Last active Sep 5, 2018
The ways to create Java threads
View App.java
package lecture1;
public class App{
public static void main(String[] args){
Runner1 r1 = new Runner1();
Runner2 r2 = new Runner2();
r1.start();
r2.start();
View GasStation.java
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
jingz8804 / WordLadder.java
Created Aug 13, 2014
#leetcode wordladder
View WordLadder.java
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
View ValidParenthesis.java
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;
View TwoSum.java
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
View MergeSort.java
// 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;
View PrintPath.java
// 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.
You can’t perform that action at this time.