From 2817b9f1fd55388f0a9bd134947f15190f613734 Mon Sep 17 00:00:00 2001 From: louissheng Date: Wed, 13 Jan 2016 12:04:34 -0800 Subject: [PATCH 01/18] Create HW1-Remove Duplicates from Sorted List II.java --- ...Remove Duplicates from Sorted List II.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 louis/HW1-Remove Duplicates from Sorted List II.java diff --git a/louis/HW1-Remove Duplicates from Sorted List II.java b/louis/HW1-Remove Duplicates from Sorted List II.java new file mode 100644 index 0000000..9932d1a --- /dev/null +++ b/louis/HW1-Remove Duplicates from Sorted List II.java @@ -0,0 +1,31 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ +public class Solution { + public ListNode deleteDuplicates(ListNode head) { + if( head == null ) + return head; + + ListNode fakehead = new ListNode(0); + fakehead.next = head; + // starts from beginning + head = fakehead; + + while (head.next != null && head.next.next != null) { + if (head.next.val == head.next.next.val) { + int temp = head.next.val; + while (head.next != null && head.next.val == temp) { + head.next = head.next.next; + } + } else { + head = head.next; + } + } + return fakehead.next; + } +} From f024c6db711ffcf4870be21f050df4e8adc36316 Mon Sep 17 00:00:00 2001 From: louissheng Date: Wed, 13 Jan 2016 12:06:18 -0800 Subject: [PATCH 02/18] Create HW1-Reverse Linked List II.java --- louis/HW1-Reverse Linked List II.java | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 louis/HW1-Reverse Linked List II.java diff --git a/louis/HW1-Reverse Linked List II.java b/louis/HW1-Reverse Linked List II.java new file mode 100644 index 0000000..e52e8da --- /dev/null +++ b/louis/HW1-Reverse Linked List II.java @@ -0,0 +1,37 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ + public class Solution { + public ListNode reverseBetween(ListNode head, int m, int n) { + if ( head == null ) { + return head; + } + + ListNode fakehead = new ListNode(0); + fakehead.next = head; + head = fakehead; + + for ( int i = 1; i < m; i++ ) { + head = head.next; + } + + ListNode beforen = head; + ListNode mNode = head.next; + ListNode nNode = head.next; + ListNode aftern = head.next.next; + for ( int i = m; i < n; i++ ) { + ListNode t = aftern.next; + aftern.next = nNode; + nNode = aftern; + aftern = t; + } + beforen.next = nNode; + mNode.next = aftern; + return fakehead.next; + } +} From 24988fac422a804c4cb6177bad03f78ac5ec251f Mon Sep 17 00:00:00 2001 From: louissheng Date: Wed, 13 Jan 2016 12:06:47 -0800 Subject: [PATCH 03/18] Create HW1-Merge Two Sorted Lists.java --- louis/HW1-Merge Two Sorted Lists.java | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 louis/HW1-Merge Two Sorted Lists.java diff --git a/louis/HW1-Merge Two Sorted Lists.java b/louis/HW1-Merge Two Sorted Lists.java new file mode 100644 index 0000000..f01a075 --- /dev/null +++ b/louis/HW1-Merge Two Sorted Lists.java @@ -0,0 +1,31 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ +public class Solution { + public ListNode mergeTwoLists(ListNode l1, ListNode l2) { + ListNode l = new ListNode(0); + ListNode p = l; + while (l1 != null && l2 != null) { + if ( l1.val <= l2.val) { + p.next = l1; + l1 = l1.next; + } else { + p.next = l2; + l2 = l2.next; + } + p = p.next; + } + if ( l1 != null ) { + p.next = l1; + } + if ( l2 != null ) { + p.next = l2; + } + return l.next; + } +} From 060fcf766af9c668ef191808c490801e5a9db345 Mon Sep 17 00:00:00 2001 From: louissheng Date: Thu, 21 Jan 2016 20:46:05 -0800 Subject: [PATCH 04/18] Create Generate Parentheses.java --- louis/Generate Parentheses.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 louis/Generate Parentheses.java diff --git a/louis/Generate Parentheses.java b/louis/Generate Parentheses.java new file mode 100644 index 0000000..b11cb3a --- /dev/null +++ b/louis/Generate Parentheses.java @@ -0,0 +1,24 @@ +public class Solution { + public List generateParenthesis(int n) { + List res = new ArrayList (); + String s = ""; + DFS(res, s, n, n, n*2); + return res; + } + private void DFS(List res, String s, int left, int right, int n) { + if (left > right) { + return; + } + if (n == 0) { + res.add(s); + return; + } + if (left > 0) { + DFS(res, s+"(", left-1, right, n-1); + } + if (right > 0) { + DFS(res, s+")", left, right -1, n-1); + } + + } +} From 3a103a0dd41b5c5661c78086a89d54e4e47c137e Mon Sep 17 00:00:00 2001 From: louissheng Date: Thu, 21 Jan 2016 20:46:36 -0800 Subject: [PATCH 05/18] Create Combination Sum III.java --- louis/Combination Sum III.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 louis/Combination Sum III.java diff --git a/louis/Combination Sum III.java b/louis/Combination Sum III.java new file mode 100644 index 0000000..3b2d089 --- /dev/null +++ b/louis/Combination Sum III.java @@ -0,0 +1,19 @@ +public class Solution { + public List> combinationSum3(int k, int n) { + List> res = new ArrayList> (); + List path = new ArrayList (); + helper(res, path, n, 1, k); + return res; + } + private void helper(List> res, List path, int n, int index, int k) { + if (n == 0 && path.size() == k) { + res.add(new ArrayList (path)); + } + for (int j = index; j <= 9; j++) { + if (j > n || path.size() > k) break; + path.add(j); + helper(res, path, n - j, j + 1, k); + path.remove(path.size() - 1); + } + } +} From a62b6326f2cfdf45dfc3f4e0ad9840b192b80f86 Mon Sep 17 00:00:00 2001 From: louissheng Date: Thu, 21 Jan 2016 20:48:18 -0800 Subject: [PATCH 06/18] Create Combination Sum II.java --- louis/Combination Sum II.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 louis/Combination Sum II.java diff --git a/louis/Combination Sum II.java b/louis/Combination Sum II.java new file mode 100644 index 0000000..53b9249 --- /dev/null +++ b/louis/Combination Sum II.java @@ -0,0 +1,29 @@ +public class Solution { + public List> combinationSum2(int[] candidates, int target) { + List> res = new ArrayList>(); + List path = new ArrayList(); + if (candidates == null || candidates.length == 0) { + return res; + } + Arrays.sort(candidates); + helper(candidates, path, 0, res, target); + return res; + } + private void helper(int[] candidates, List path, int index, List> res, int target) { + if (target == 0) { + res.add(new ArrayList(path)); + } + if (index == candidates.length) { + return; + } + int prev = -1; + for (int i = index; i < candidates.length; i++) { + if (candidates[i] > target) break; + if (candidates[i] == prev) continue; + path.add(candidates[i]); + helper(candidates, path, i+1, res, target-candidates[i]); + prev = candidates[i]; + path.remove(path.size() - 1); + } + } +} From 2462db92c86add7d9e8a0bc49e16a3a86b3fc55a Mon Sep 17 00:00:00 2001 From: louissheng Date: Thu, 21 Jan 2016 20:48:50 -0800 Subject: [PATCH 07/18] Create Combination Sum.java --- louis/Combination Sum.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 louis/Combination Sum.java diff --git a/louis/Combination Sum.java b/louis/Combination Sum.java new file mode 100644 index 0000000..2934f6f --- /dev/null +++ b/louis/Combination Sum.java @@ -0,0 +1,26 @@ +public class Solution { + public List> combinationSum(int[] candidates, int target) { + List> res = new ArrayList>(); + List path = new ArrayList(); + if (candidates == null || candidates.length == 0) { + return res; + } + Arrays.sort(candidates); + helper(candidates, path, 0, res, target); + return res; + } + private void helper(int[] candidates, List path, int index, List> res, int target) { + if (target == 0) { + res.add(new ArrayList(path)); + } + if (index == candidates.length) { + return; + } + for (int i = index; i < candidates.length; i++) { + if (candidates[i] > target) break; + path.add(candidates[i]); + helper(candidates, path, i, res, target-candidates[i]); + path.remove(path.size() - 1); + } + } +} From 17531a20fe758a92ab5160ba475a96b90076b93c Mon Sep 17 00:00:00 2001 From: louissheng Date: Thu, 21 Jan 2016 20:49:28 -0800 Subject: [PATCH 08/18] Create Subsets.java --- louis/Subsets.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 louis/Subsets.java diff --git a/louis/Subsets.java b/louis/Subsets.java new file mode 100644 index 0000000..c6a2af4 --- /dev/null +++ b/louis/Subsets.java @@ -0,0 +1,23 @@ +public class Solution { + public List> subsets(int[] nums) { + List> res = new ArrayList>(); + if (nums == null || nums.length == 0) { + return res; + } + List path = new ArrayList(); + Arrays.sort(nums); + helper(res, path, nums, 0); + return res; + } + public void helper(List> res, List path, int[] nums, int index) { + res.add(new ArrayList(path)); + if (index >= nums.length) { + return; + } + for (int i = index; i < nums.length; i++) { + path.add(nums[i]); + helper(res, path, nums, i + 1); + path.remove(path.size() - 1); + } + } +} From cc39e6f3cb3c13078401ee0945ee46645ffe08ab Mon Sep 17 00:00:00 2001 From: louissheng Date: Thu, 21 Jan 2016 20:51:08 -0800 Subject: [PATCH 09/18] Create HW2-Binary Tree Path --- louis/HW2-Binary Tree Path | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 louis/HW2-Binary Tree Path diff --git a/louis/HW2-Binary Tree Path b/louis/HW2-Binary Tree Path new file mode 100644 index 0000000..fc4af73 --- /dev/null +++ b/louis/HW2-Binary Tree Path @@ -0,0 +1,32 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +public class Solution { + public List binaryTreePaths(TreeNode root) { + List ret = new ArrayList(); + if (root == null) { + return ret; + } + dfs(root, ret, String.valueOf(root.val)); + return ret; + } + private void dfs(TreeNode root, List ret, String path) { + if (root.left == null && root.right == null) { + ret.add(path); + return; + } + if (root.left != null) { + dfs(root.left, ret, path + "->" + root.left.val); + } + if (root.right != null) { + dfs(root.right, ret, path + "->" + root.right.val); + } + } + +} From 9a5d2cd863de01808d96834de383f29ce8c3cd60 Mon Sep 17 00:00:00 2001 From: louissheng Date: Thu, 21 Jan 2016 20:51:59 -0800 Subject: [PATCH 10/18] Create HW2-Unique Binary Search Tree.java --- louis/HW2-Unique Binary Search Tree.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 louis/HW2-Unique Binary Search Tree.java diff --git a/louis/HW2-Unique Binary Search Tree.java b/louis/HW2-Unique Binary Search Tree.java new file mode 100644 index 0000000..e111c0d --- /dev/null +++ b/louis/HW2-Unique Binary Search Tree.java @@ -0,0 +1,12 @@ +public class Solution { + public int numTrees(int n) { + int[] count = new int[n + 1]; + count[0] = 1; + for (int i = 1; i <= n; i++) { + for (int j = 0; j < i; j++) { + count[i] += count[j]*count[i-j-1]; + } + } + return count[n]; + } +} From 5996a9b3e16ed81da73f9eb0a866d1fd549e3be7 Mon Sep 17 00:00:00 2001 From: louissheng Date: Thu, 21 Jan 2016 20:52:45 -0800 Subject: [PATCH 11/18] Create HW2-Validate Binary Search Tree.java --- louis/HW2-Validate Binary Search Tree.java | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 louis/HW2-Validate Binary Search Tree.java diff --git a/louis/HW2-Validate Binary Search Tree.java b/louis/HW2-Validate Binary Search Tree.java new file mode 100644 index 0000000..bbc425a --- /dev/null +++ b/louis/HW2-Validate Binary Search Tree.java @@ -0,0 +1,23 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +public class Solution { + public boolean isValidBST(TreeNode root) { + return helper(root, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); + } + private boolean helper(TreeNode root, double min, double max) { + if (root == null) { + return true; + } + if (min < root.val && root.val < max) { + return helper(root.left, min, root.val) && helper(root.right, root.val, max); + } else return false; + + } +} From cdb31092c1aa4810f14b935bbb73efebd3474e1b Mon Sep 17 00:00:00 2001 From: louissheng Date: Thu, 21 Jan 2016 20:53:09 -0800 Subject: [PATCH 12/18] Create HW2-Maximum Depth of Binary Tree.java --- louis/HW2-Maximum Depth of Binary Tree.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 louis/HW2-Maximum Depth of Binary Tree.java diff --git a/louis/HW2-Maximum Depth of Binary Tree.java b/louis/HW2-Maximum Depth of Binary Tree.java new file mode 100644 index 0000000..1a4b8a6 --- /dev/null +++ b/louis/HW2-Maximum Depth of Binary Tree.java @@ -0,0 +1,17 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +public class Solution { + public int maxDepth(TreeNode root) { + if (root == null) { + return 0; + } + return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; + } +} From c6a89a3ab9236453a49d187814eedf1c5f913ff5 Mon Sep 17 00:00:00 2001 From: louissheng Date: Thu, 21 Jan 2016 20:53:47 -0800 Subject: [PATCH 13/18] Create HW2-Symmetric Tree.java --- louis/HW2-Symmetric Tree.java | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 louis/HW2-Symmetric Tree.java diff --git a/louis/HW2-Symmetric Tree.java b/louis/HW2-Symmetric Tree.java new file mode 100644 index 0000000..41bff42 --- /dev/null +++ b/louis/HW2-Symmetric Tree.java @@ -0,0 +1,36 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +public class Solution { + public boolean isSymmetric(TreeNode root) { + if (root == null) { + return true; + } else { + return isSymmetric(root.left, root.right); + } + } + public boolean isSymmetric(TreeNode l, TreeNode r) { + if (l == null && r == null) { + return true; + } + if (l!= null && r == null) { + return false; + } + if (l==null && r!=null) { + return false; + } + if (l.val != r.val){ + return false; + } + if (!isSymmetric(l.left, r.right) || !isSymmetric(l.right,r.left)) { + return false; + } + return true; + } +} From bc25779b5920323b6d68a1da6d0ba0223470d9f8 Mon Sep 17 00:00:00 2001 From: louissheng Date: Thu, 21 Jan 2016 20:54:19 -0800 Subject: [PATCH 14/18] Create HW2-Convert Sorted Array to Binary Search Tree.java --- ...rt Sorted Array to Binary Search Tree.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 louis/HW2-Convert Sorted Array to Binary Search Tree.java diff --git a/louis/HW2-Convert Sorted Array to Binary Search Tree.java b/louis/HW2-Convert Sorted Array to Binary Search Tree.java new file mode 100644 index 0000000..4cc81ae --- /dev/null +++ b/louis/HW2-Convert Sorted Array to Binary Search Tree.java @@ -0,0 +1,26 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +public class Solution { + public TreeNode sortedArrayToBST(int[] nums) { + if (nums == null) { + return null; + }; + return convert(nums, 0, nums.length-1); + } + public TreeNode convert(int[] nums, int start, int end) { + if (start > end) { + return null; + } + TreeNode mid = new TreeNode(nums[start + (end-start)/2]); + mid.left = convert(nums, start, start + (end-start)/2 - 1); + mid.right = convert(nums, start + (end-start)/2 +1, end); + return mid; + } +} From 99aa819a53d9ab5f9e4371da912b62bd7eee1e93 Mon Sep 17 00:00:00 2001 From: louissheng Date: Thu, 21 Jan 2016 20:55:03 -0800 Subject: [PATCH 15/18] Create HW2-Construct Binary Tree from Preorder and Inorder Traversal.java --- ...e from Preorder and Inorder Traversal.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 louis/HW2-Construct Binary Tree from Preorder and Inorder Traversal.java diff --git a/louis/HW2-Construct Binary Tree from Preorder and Inorder Traversal.java b/louis/HW2-Construct Binary Tree from Preorder and Inorder Traversal.java new file mode 100644 index 0000000..469c8b4 --- /dev/null +++ b/louis/HW2-Construct Binary Tree from Preorder and Inorder Traversal.java @@ -0,0 +1,32 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +public class Solution { + private int findPosition(int[] arr, int start, int end, int key) { + for (int i = start;i<= end;i++) { + if (arr[i] == key) { + return i; + } + } + return -1; + } + public TreeNode buildTree(int[] preorder, int[] inorder) { + return myBuild(preorder, 0, preorder.length-1, inorder, 0, inorder.length -1 ); + } + private TreeNode myBuild(int[] preorder, int prestart, int preend, int[] inorder, int instart, int inend) { + if (instart > inend) { + return null; + } + TreeNode root = new TreeNode(preorder[prestart]); + int position = findPosition(inorder, instart, inend, root.val); + root.left = myBuild(preorder, prestart+1, prestart + position - instart, inorder, instart, position - 1); + root.right = myBuild(preorder, prestart + position - instart +1, preend, inorder, position + 1, inend); + return root; + } +} From 0f9406c61ce9dd4d1a1cabf6ac2e5c8b1078cc65 Mon Sep 17 00:00:00 2001 From: louissheng Date: Thu, 21 Jan 2016 20:55:40 -0800 Subject: [PATCH 16/18] Create HW2-Construct Binary Tree from Inorder and Postorder Traversal.java --- ... from Inorder and Postorder Traversal.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 louis/HW2-Construct Binary Tree from Inorder and Postorder Traversal.java diff --git a/louis/HW2-Construct Binary Tree from Inorder and Postorder Traversal.java b/louis/HW2-Construct Binary Tree from Inorder and Postorder Traversal.java new file mode 100644 index 0000000..3b0b40d --- /dev/null +++ b/louis/HW2-Construct Binary Tree from Inorder and Postorder Traversal.java @@ -0,0 +1,34 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +public class Solution { + private int findPosition(int[] arr, int start, int end, int key) { + int i; + for (i = start; i <= end; i++) { + if (arr[i] == key) { + return i; + } + } + return -1; + } + private TreeNode helper(int[] inorder, int instart, int inend, + int[] postorder, int poststart, int postend) { + if (instart > inend) return null; + TreeNode root = new TreeNode(postorder[postend]); + int position = findPosition(inorder, instart, inend, root.val); + root.left = helper(inorder, instart, position - 1, postorder, poststart, poststart + position - instart - 1); + root.right = helper(inorder, position + 1, inend, postorder, poststart + position - instart, postend - 1); + return root; + } + + public TreeNode buildTree(int[] inorder, int[] postorder) { + return helper(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1); + } + +} From b68e5d33c38b4b5e679ef9bfb57ad995146b23e0 Mon Sep 17 00:00:00 2001 From: louissheng Date: Thu, 21 Jan 2016 20:56:11 -0800 Subject: [PATCH 17/18] Create HW2-Lowest Common Ancestor of a Binary Tree.java --- ...west Common Ancestor of a Binary Tree.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 louis/HW2-Lowest Common Ancestor of a Binary Tree.java diff --git a/louis/HW2-Lowest Common Ancestor of a Binary Tree.java b/louis/HW2-Lowest Common Ancestor of a Binary Tree.java new file mode 100644 index 0000000..2f99a5b --- /dev/null +++ b/louis/HW2-Lowest Common Ancestor of a Binary Tree.java @@ -0,0 +1,30 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +public class Solution { + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + if (root == p || root == q || root == null) { + return root; + } + + TreeNode left = lowestCommonAncestor(root.left, p, q); + TreeNode right = lowestCommonAncestor(root.right, p, q); + + if (left != null && right != null) { + return root; + } + if (left != null) { + return left; + } + if (right != null) { + return right; + } + return null; + } +} From 169a36eac70f86d1562e9091432baf886a48b773 Mon Sep 17 00:00:00 2001 From: louissheng Date: Thu, 21 Jan 2016 20:56:28 -0800 Subject: [PATCH 18/18] Create HW2-Lowest Common Ancestor of a Binary Search Tree.java --- ...ommon Ancestor of a Binary Search Tree.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 louis/HW2-Lowest Common Ancestor of a Binary Search Tree.java diff --git a/louis/HW2-Lowest Common Ancestor of a Binary Search Tree.java b/louis/HW2-Lowest Common Ancestor of a Binary Search Tree.java new file mode 100644 index 0000000..4d638b3 --- /dev/null +++ b/louis/HW2-Lowest Common Ancestor of a Binary Search Tree.java @@ -0,0 +1,18 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +public class Solution { + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + if (root.val > p.val && root.val > q.val) { + return lowestCommonAncestor(root.left, p, q); + } else if (root.val < p.val && root.val < q.val) { + return lowestCommonAncestor(root.right, p, q); + } else return root; + } +}