Skip to content

Instantly share code, notes, and snippets.

View daifu's full-sized avatar

Daifu Richard Ye daifu

View GitHub Profile
@daifu
daifu / permuteUnique.java
Created April 25, 2013 05:06
Permutation II
/*
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
[1,1,2] have the following unique permutations:
[1,1,2], [1,2,1], and [2,1,1].
Algorithm:
Basically it is similar to Permutation, but it needs
to check the condition that if the current num is the
@daifu
daifu / permute.java
Created April 25, 2013 02:42
Permutation
/*
Given a collection of numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
Algorithm:
1. check if the list is equal to the size of the num array
2. loop through all the possibility
@daifu
daifu / rotate.java
Created April 24, 2013 07:02
Roate Image
/*
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
Basic approach has 2 steps:
1. rotate based on the right most to left bottom diagonal
@daifu
daifu / atoi.java
Created April 22, 2013 21:51
Implement atoi to convert a string to an integer.
/*
Cases need to be taken care of:
1. sign
2. extra space in the front
3. half valid number
4. overflow for the large number
5. invalid number
*/
public class Solution {
public int atoi(String str) {
@daifu
daifu / buildTree.java
Last active December 16, 2015 12:58
Construct Binary Tree from Preorder and Inorder Traversal
/*
Given preorder and inorder 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 / grayCode.java
Created April 22, 2013 14:51
Gray Code
/*
The gray code is a binary numeral system where two successive values differ in only one bit.
Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.
For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:
00 - 0
01 - 1
11 - 3
@daifu
daifu / numDecodings.java
Created April 22, 2013 06:13
Decode Ways
/*
A message containing letters from A-Z is being encoded to numbers using the following mapping:
'A' -> 1
'B' -> 2
...
'Z' -> 26
Given an encoded message containing digits, determine the total number of ways to decode it.
For example,
@daifu
daifu / getRow.java
Created April 21, 2013 23:42
Pascal's Triangle II
/*
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1].
Note:
Could you optimize your algorithm to use only O(k) extra space?
*/
public class Solution {
@daifu
daifu / generate.java
Created April 21, 2013 23:21
Pascal's Triangle
/*
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
@daifu
daifu / combine.java
Created April 18, 2013 16:40
Combinations
/*
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[
[2,4],
[3,4],
[2,3],