Skip to content

Instantly share code, notes, and snippets.

View daifu's full-sized avatar

Daifu Richard Ye daifu

View GitHub Profile
@daifu
daifu / search.java
Last active December 16, 2015 19:39
Search in Rotated Sorted Array II
/*
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
Algorithm:
@daifu
daifu / search.java
Created April 30, 2013 05:54
Search in Rotated Sorted Array
/*
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
Algorithm:
@daifu
daifu / plusOne.java
Last active December 16, 2015 18:49
Plus One
/*
Given a number represented as an array of digits, plus one to the number.
*/
public class Solution {
public int[] plusOne(int[] digits) {
// Start typing your Java solution below
// DO NOT write main() function
int len = digits.length - 1;
digits[len] = digits[len] + 1;
int carry = digits[len] >= 10 ? 1 : 0;
@daifu
daifu / merge.java
Created April 29, 2013 00:56
Merge Sorted Array
/*
Merged two sorted array.
merged B into A
*/
public class Solution {
public void merge(int A[], int m, int B[], int n) {
// Start typing your Java solution below
// DO NOT write main() function
@daifu
daifu / isSameTree.java
Created April 28, 2013 06:32
Same Tree
/*
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
Algorithm:
1. It is a recursive function that check p.left and q.left,
and p.right and p.right, and p.val and q.val
*/
@daifu
daifu / buildTree.java
Created April 27, 2013 07:42
Construct Binary Tree from Inorder and Postorder Traversal
/*
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
*/
/**
* Definition for binary tree
* public class TreeNode {
* int val;
@daifu
daifu / isSymmetric.java
Created April 26, 2013 16:49
Symmetric Tree
/*
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
/*
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree {3,9,20,#,#,15,7},
3
/ \
9 20
/ \
/*
Given a collection of integers that might contain duplicates, S, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If S = [1,2,2], a solution is:
@daifu
daifu / prettyPrintTree.java
Last active December 16, 2015 15:49
Pretty Print a Binary Search Tree
static class Wrapper {
TreeNode node;
int level;
Wrapper(TreeNode node, int level) {
this.node = node;
this.level = level;
}
}
static int skip;