-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathPascalsTriangle.java
65 lines (46 loc) · 1.55 KB
/
PascalsTriangle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// https://leetcode.com/problems/pascals-triangle/
//Soltion 1 @Coding Decoded
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> ans = new ArrayList<>();
//
int row = 1;
List<Integer> subList = new ArrayList<>();
subList.add(1);
ans.add(subList);
while(row<numRows){
List<Integer> previousList = ans.get(row-1);
List<Integer> newList = new ArrayList<>();
for(int i=0;i<=row;i++){
int leftEntry = i>=1 ? previousList.get(i - 1) :0;
int rightEntry = i<row ? previousList.get(i) :0;
newList.add(leftEntry + rightEntry);
}
ans.add(newList);
row++;
}
return ans;
}
}
// Solution 2 Thanks @theankitroy
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> ans=new ArrayList<>();
for(int i=0;i<numRows;i++){
ans.add(pascalTriangle2(i));
}
return ans;
}
// Logic explanation : https://github.com/Sunchit/Coding-Decoded/blob/master/June2021/img/PascalsTriangleCombinationApproach.png
public static ArrayList<Integer> pascalTriangle2(int r){
ArrayList<Integer> ans=new ArrayList<>();
int i=r;
int val=1;
//(i-j)/(j+1) is the factor need to multiply with current value to get the next value.
for(int j=0;j<=r;j++){
ans.add(val);
val=val*(i-j)/(j+1);
}
return ans;
}
}