Skip to content

Instantly share code, notes, and snippets.

View daifu's full-sized avatar

Daifu Richard Ye daifu

View GitHub Profile
@daifu
daifu / minDepth.java
Created February 26, 2013 01:48
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
@daifu
daifu / permuteUnique.java
Last active December 14, 2015 05:40
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
/*
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].
*/
public class Solution {
public boolean[] used;
public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
@daifu
daifu / isSameTree.java
Created February 26, 2013 08:55
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.
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
@daifu
daifu / deleteDuplicates.java
Created March 1, 2013 02:48
Given a sorted linked list, delete all duplicates such that each element appear only once.
/**
* Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
@daifu
daifu / partition.java
Last active December 14, 2015 12:39
Given a string s, partition s such that every substring of the partition is a palindrome.
/*
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab",
Return
[
["aa","b"],
["a","a","b"]
@daifu
daifu / sumNumbers.java
Created March 5, 2013 04:55
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
/*
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path 1->2->3 which represents the number 123.
Find the total sum of all root-to-leaf numbers.
For example,
1
@daifu
daifu / longestConsecutive.java
Created March 5, 2013 08:21
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
/*
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
For example,
Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
*/
public class Solution {
public int longestConsecutive(int[] num) {
@daifu
daifu / ladderLength.java
Last active December 14, 2015 12:48
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:
/*
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:
Only one letter can be changed at a time
Each intermediate word must exist in the dictionary
For example,
Given:
start = "hit"
end = "cog"
@daifu
daifu / mergeIntervals.java
Created March 5, 2013 23:29
Given a collection of intervals, merge all overlapping intervals.
/*
Given a collection of intervals, merge all overlapping intervals.
For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18]
*/
/**
* Definition for an interval.
@daifu
daifu / getRange.java
Created March 6, 2013 05:58
Given a sorted integer array and a key, output its indices’ range.
/*
Given a sorted integer array and a key, output its indices’ range.
For example,
[5, 7, 7, 8, 8, 10] –> Given 8, outputs [3, 4]
*/
public class Untitled {
// get a range of position of sorted array
public int[] getRange(int[] input, int target) {
int upper = getUpperBound(input, target);