-
Notifications
You must be signed in to change notification settings - Fork 0
/
118_PascalTriangle.java
52 lines (49 loc) · 1.38 KB
/
118_PascalTriangle.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
/*
* 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]
* ]
*/
//K(i)(j)=K(i-1)(j-1)+K(i-1)(j) except for the first and last element
public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> triangle = new ArrayList<List<Integer>>();
if (numRows <=0){
return triangle;
}
for (int i=0; i<numRows; i++){
List<Integer> row = new ArrayList<Integer>();
for (int j=0; j<i+1; j++){
if (j==0 || j==i){
row.add(1);
} else {
row.add(triangle.get(i-1).get(j-1)+triangle.get(i-1).get(j));
}
}
triangle.add(row);
}
return triangle;
}
}
//Math dont understand
public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
for (int n = 0; n < numRows; n++) {
int nCk = 1;
List<Integer> list = new ArrayList<Integer>();
for (int k = 0; k <= n; k++) {
list.add(nCk);
nCk = nCk * (n - k) / (k + 1);
}
result.add(list);
}
return result;
}
}