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 NaiveUnionFind{ | |
private int[] id; | |
public NaiveUnionFind(int N){ | |
id = new int[N]; | |
for (int i = 0; i < N; i++){ | |
id[i] = 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.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){ |
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.Iterator; | |
import java.util.NoSuchElementException; | |
public class LinkedListStack<Item> implements Iterable<Item> { | |
private class Node { | |
Item value; | |
Node next; | |
} | |
private Node top; |
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.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; | |
} |
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.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 |
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.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(){ |
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.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); |
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.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 |
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 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 |
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.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(){ |
OlderNewer