Skip to content

fix format for 118 Pascal's Triangle #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
55 changes: 55 additions & 0 deletions Easy/Pascal_Triangle.java
Original file line number Diff line number Diff line change
@@ -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<List<Integer>> generate(int numRows) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
ArrayList<Integer> member = new ArrayList<Integer>();
if(numRows <= 0){
return res;
}
member.add(1);
res.add( member);
return generateArray(res, numRows-1);
}

public static List<List<Integer>> generateArray(List<List<Integer>> res, int n){
if(n == 0){
return res;
}
else{
List<Integer> resLast= new ArrayList<Integer>();
ArrayList<Integer> newMember = new ArrayList<Integer>();
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));
}
}
47 changes: 23 additions & 24 deletions Medium/BinaryTreePreorderTraversal.java
Original file line number Diff line number Diff line change
@@ -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
*/
Expand All @@ -31,6 +31,7 @@ public static List<Integer> preorderTraversal(TreeNode root) {
preorderTraversalTree(root, res);
return res;
}

public static void preorderTraversalTree(TreeNode root, List<Integer> res){
if(root != null){
res.add(root.val);
Expand Down Expand Up @@ -66,16 +67,14 @@ public static List<Integer> 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));
}

}
44 changes: 44 additions & 0 deletions Medium/RemoveDuplicatesfromSortedArray2.java
Original file line number Diff line number Diff line change
@@ -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<nums.length; i++){
if(nums[i] == nums[i-1] && countEach < 2){
nums[res] = nums[i];
countEach++;
res++;
}
else if(nums[i] != nums[i-1]){
nums[res] = nums[i];
res++;
countEach = 1;
}
else{
countEach++;
}
}
return res;
}

public static void main(String[] args) {
int[] res = {1,1,1,2,3};
System.out.print(removeDuplicates(res));
}
}