Skip to content

Instantly share code, notes, and snippets.

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(){
// 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 :)
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;
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);
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) {
@jingz8804
jingz8804 / RandomInteger.java
Created March 21, 2014 20:27
Select a random integer from an array with predefined probability
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;
@jingz8804
jingz8804 / Evaluate.java
Created March 21, 2014 22:03
the Evaluate class in the book Algorithms 4th edition with slight modification
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>();
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }