-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
54 lines (36 loc) · 1.19 KB
/
Solution.java
File metadata and controls
54 lines (36 loc) · 1.19 KB
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
package leetcode.pascalTriagle;
import java.util.ArrayList;
import java.util.List;
public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> pascal = new ArrayList<>();
if(numRows == 0){
return pascal;
}
if(numRows == 1){
List<Integer> firstRow = new ArrayList<>();
firstRow.add(1);
pascal.add(firstRow);
return pascal;
}
pascal = generate(numRows-1);
List<Integer> prevList = pascal.get(numRows-2);
List<Integer> currentRow = new ArrayList<>();
currentRow.add(1);
for(int i = 1; i < numRows-1; i++){
int number = prevList.get(i-1) + prevList.get(i);
currentRow.add(number);
}
currentRow.add(1);
pascal.add(currentRow);
return pascal;
}
public static void main(String[] args) {
Solution s = new Solution();
int test = 4;
String resultExpected = "bb";
List<List<Integer>> result = s.generate(4);
System.out.println("RESULT EXPECTED: " + resultExpected);
System.out.println("RESULT: " + result );
}
}