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(){ |
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
// in this context, we've already know that the list has cycle | |
// suppose there is a class Node | |
// public class Node<Item>{ | |
// Item item; | |
// Node next; | |
// } | |
// import the Node class here; | |
public class DetectCycleStartInList{// I wish I could use a better name :) | |
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 CloneDoubleLinkedList{ | |
private class Node{ | |
private String item; | |
private Node next; | |
private Node random; | |
} | |
public Node cloneListClever(Node first){ | |
Node current = first; |
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 SimplifyPath { | |
public String simplifyPath(String path) { | |
if(path == null) return null; | |
Stack<String> s = new Stack<String>(); | |
int len = path.length(); | |
StringBuilder b = new StringBuilder(); | |
String item = null; | |
for (int i = 1; i < len; i++){ // start with the char at index 1 since the first is always '/' | |
char c = path.charAt(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.Stack; | |
import java.util.LinkedList; | |
public class SymmetricTree { | |
public boolean isSymmetricQueue(TreeNode root) { | |
if (root == null) return true; // if you say so | |
// initialize a queue since we are going to use breadth first traverse | |
LinkedList<TreeNode> q = new LinkedList<TreeNode>(); | |
if (root.left !=null && root.right != null) { |
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.Random; | |
public class RandomItem{ | |
private int[] numbers; | |
private double[] probs; | |
private Random rand; | |
public RandomItem(int[] numbers, double[] probs){ | |
this.numbers = numbers; | |
this.probs = probs; |
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 Evaluate{ | |
private Stack<Double> operands; | |
private Stack<String> operators; | |
public Evaluate(){ | |
operands = new Stack<Double>(); | |
operators = new Stack<String>(); | |
} | |
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
/** | |
* Definition for singly-linked list. | |
* public class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { | |
* val = x; | |
* next = null; | |
* } | |
* } |