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); + } + } +} 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); + } + } +} 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); + } + } +} 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); + } + + } +} 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; + } +} 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; + } +} 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; + } +} 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); + } + } + +} 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); + } + +} 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; + } +} 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; + } +} 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; + } +} 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; + } +} 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; + } +} 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; + } +} 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]; + } +} 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; + + } +} 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); + } + } +}