Skip to content

Instantly share code, notes, and snippets.

View daifu's full-sized avatar

Daifu Richard Ye daifu

View GitHub Profile
@daifu
daifu / hasPathSum.java
Created January 27, 2013 11:29
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*
For example:
public class Solution {
public ArrayList<ArrayList<Integer>> permute(int[] num) {
// Start typing your Java solution below
// DO NOT write main() function
if (num.length == 0) return null;
ArrayList<Integer> intList = new ArrayList<Integer>();
ArrayList<Integer> prefix = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
for (int index = 0; index < num.length; index++)
{
@daifu
daifu / atoi.java
Created February 1, 2013 03:42
Implement atoi to convert a string to an integer.
public class Solution {
public int atoi(String str) {
// Start typing your Java solution below
// DO NOT write main() function
// string start with -
// string is overflow the Integer.MAX_VALUE
if(str.length() == 0) return 0;
int right = str.length() - 1;
int res = 0;
@daifu
daifu / longestPalindrome.java
Last active April 20, 2020 10:13
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
public class Solution {
static int EVEN = 1;
static int ODD = 2;
public String longestPalindrome(String s) {
// Start typing your Java solution below
// DO NOT write main() function
// Test cases:
// a
// aa
@daifu
daifu / nextPermutation.java
Created February 7, 2013 08:06
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
/*
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
*/
@daifu
daifu / combinationSum2.java
Created February 12, 2013 08:29
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
/*
For example, given candidate set 10,1,2,7,6,1,5 and target 8,
A solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]
*/
public class Solution {
@daifu
daifu / getPermutation.java
Created February 15, 2013 05:31
Given n and k, return the kth permutation sequence.
/*
The set [1,2,3,…,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
"123"
"132"
"213"
"231"
@daifu
daifu / partition.java
Created February 15, 2013 07:02
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
/*
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.
*/
public ListNode partition(ListNode head, int x) {
@daifu
daifu / longestCommonPrefix.java
Created February 25, 2013 18:53
Write a function to find the longest common prefix string amongst an array of strings.
public class Solution {
public String longestCommonPrefix(String[] strs) {
// Start typing your Java solution below
// DO NOT write main() function
int size = strs.length;
if(size == 0) {
return "";
}
String common = strs[0];
@daifu
daifu / triTiling.java
Created February 26, 2013 01:25
Determine in how many ways can a 3xN rectangle be completely tiled with 2x1 dominoes.
public int triTiling() {
if (n == 0) {
return 1;
}
int[] f = new int[n];
int[] g = new int[n];
f[0] = 1;
f[1] = 0;
g[1] = 1;
g[0] = 0;