diff --git a/Easy/Pascal_Triangle.java b/Easy/Pascal_Triangle.java new file mode 100644 index 00000000..2503b32e --- /dev/null +++ b/Easy/Pascal_Triangle.java @@ -0,0 +1,55 @@ +import java.util.*; +/** + * 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], + * [1,3,3,1], + * [1,4,6,4,1] + * ] + * Tags: Array + * Similar Problems: Pascal's Triangle II + * + * Analysis : using dp + * @author chenshuna + */ + +public class Pascal_Triangle { + public static List> generate(int numRows) { + List> res = new ArrayList>(); + ArrayList member = new ArrayList(); + if(numRows <= 0){ + return res; + } + member.add(1); + res.add( member); + return generateArray(res, numRows-1); + } + + public static List> generateArray(List> res, int n){ + if(n == 0){ + return res; + } + else{ + List resLast= new ArrayList(); + ArrayList newMember = new ArrayList(); + resLast = res.get(res.size()-1); + newMember.add(1); + for(int i = 0; i < resLast.size()-1; i++){ + int temp = resLast.get(i) + resLast.get(i+1); + newMember.add(temp); + } + newMember.add(1); + res.add(newMember); + return generateArray(res, n-1); + } + } + + public static void main(String[] args) { + System.out.print(generate(5)); + } +} diff --git a/Medium/BinaryTreePreorderTraversal.java b/Medium/BinaryTreePreorderTraversal.java index b80aaf85..614ed953 100644 --- a/Medium/BinaryTreePreorderTraversal.java +++ b/Medium/BinaryTreePreorderTraversal.java @@ -1,25 +1,25 @@ -package gitLeetCode; - import java.util.*; /** * Given a binary tree, return the preorder traversal of its nodes' values. - - For example: - Given binary tree {1,#,2,3}, - 1 - \ - 2 - / - 3 - return [1,2,3]. - * @author chenshuna + * For example: + * Given binary tree {1,#,2,3}, + * 1 + * \ + * 2 + * / + * 3 + * return [1,2,3]. + * Tags: Tree, Stack + * Similar Problems: Binary Tree Inorder Traversal, Verify Preorder Sequence in Binary Search Tree * * Note: Recursive solution + * @author chenshuna */ public class BinaryTreePreorderTraversal { - /** + + /** * Recursive solution * @param args */ @@ -31,6 +31,7 @@ public static List preorderTraversal(TreeNode root) { preorderTraversalTree(root, res); return res; } + public static void preorderTraversalTree(TreeNode root, List res){ if(root != null){ res.add(root.val); @@ -66,16 +67,14 @@ public static List preorderTraversalIterative(TreeNode root) { } public static void main(String[] args) { - // TODO Auto-generated method stub - TreeNode res = new TreeNode(0); - res.left = new TreeNode(2); - res.right = new TreeNode(1); - res.left.left = new TreeNode(4); - res.left.right = new TreeNode(5); - res.left.left.left = new TreeNode(9); - - System.out.print(preorderTraversal(res)); - System.out.print(preorderTraversalIterative(res)); - } + TreeNode res = new TreeNode(0); + res.left = new TreeNode(2); + res.right = new TreeNode(1); + res.left.left = new TreeNode(4); + res.left.right = new TreeNode(5); + res.left.left.left = new TreeNode(9); + System.out.print(preorderTraversal(res)); + System.out.print(preorderTraversalIterative(res)); + } } diff --git a/Medium/RemoveDuplicatesfromSortedArray2.java b/Medium/RemoveDuplicatesfromSortedArray2.java new file mode 100644 index 00000000..d90e2a92 --- /dev/null +++ b/Medium/RemoveDuplicatesfromSortedArray2.java @@ -0,0 +1,44 @@ +/** + * Remove Duplicates from Sorted Array II + * Follow up for "Remove Duplicates": + * What if duplicates are allowed at most twice? + * For example, + * Given sorted array nums = [1,1,1,2,2,3], + * Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length. + * Tag: Linkedlist + * + * Note: create a countEach variable to count duplicated times for each integer in array. + * Time complexity = O(n) + * @author chenshuna + */ + +public class RemoveDuplicatesfromSortedArray2 { + public static int removeDuplicates(int[] nums) { + int res = 1; + if(nums.length <= 1){ + return nums.length; + } + int countEach = 1; + for(int i = 1; i