Skip to content

Instantly share code, notes, and snippets.

@jingz8804
jingz8804 / NaiveUnionFind.java
Last active August 29, 2015 13:56
The naive union find algorithm. It takes O(N) time to union two objects but O(1) to check if two objects are connected.
public class NaiveUnionFind{
private int[] id;
public NaiveUnionFind(int N){
id = new int[N];
for (int i = 0; i < N; i++){
id[i] = i;
}
}
@jingz8804
jingz8804 / ThreeSum.java
Created February 28, 2014 22:32
3-Sum algorithm in O(n^2) time.
import java.util.Hashtable;
public class ThreeSum{
private int[] numbers;
private Hashtable<Integer, Boolean> numTable;
public ThreeSum(int[] numbers){
this.numbers = numbers;
numTable = new Hashtable();
for(int num: numbers){
@jingz8804
jingz8804 / LinkedListStack.java
Last active August 29, 2015 13:57
A Stack using LinkedList
import java.util.Iterator;
import java.util.NoSuchElementException;
public class LinkedListStack<Item> implements Iterable<Item> {
private class Node {
Item value;
Node next;
}
private Node top;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ResizingArrayStack<Item> implements Iterable<Item>{
private Item[] values;
private int count;
public ResizingArrayStack(){
values = (Item[]) new Object[1]; // initialize the array to hold 1 element
count = 0;
}
import java.util.NoSuchElementException;
import java.util.Iterator;
public class LinkedListQueue<Item> implements Iterable<Item>{
private class Node{
Item item;
Node next;
}
private Node head; // head of the queue; we dequeue at the head
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ResizingArrayQueue<Item> implements Iterable<Item>{
private Item[] items; // the actual array holding the items
private int head; // the index of the head of the queue
private int tail; // the index of the tail of the queue
private int count; // the number of items in the queue
public ResizingArrayQueue(){
@jingz8804
jingz8804 / Subsets.java
Last active August 29, 2015 13:57
Subsets problem on LeetCode
import java.util.Arrays;
import java.util.ArrayList;
public class Subsets{
public ArrayList<ArrayList<Integer>> subsets(int[] S) {
// sort the array first
Arrays.sort(S);
ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> emptySet = new ArrayList<Integer>();
results.add(emptySet);
import java.util.Arrays;
public class Search2DMatrix{
public boolean searchMatrixOne(int[][] matrix, int target){
int numOfRows = matrix.length;
int numOfColumns = matrix[0].length;
if (numOfRows == 0 || numOfColumns == 0) return false;
// here we assume that the total number of elements in the matrix
// is inside the range of Integer in Java
public class StockOne{
// example: [5, 4, 1, 2, 8, 6, 9]
public int maxProfit(int[] prices){
if(prices.length <= 1) return 0;
return maxProfitOne(prices);
// return maxProfitTwo(prices, 0, prices[0], 0);
// return maxProfitThree(prices, new Integer(-1), 0);
}
// this function use the iterative algorithm
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Deque<Item> implements Iterable<Item>{
private Item[] deck; // the array for the deck
private int N; // the number of items in the deck
private int head;
private int tail;
public Deque(){