-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode118.java
More file actions
31 lines (25 loc) · 853 Bytes
/
leetcode118.java
File metadata and controls
31 lines (25 loc) · 853 Bytes
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
import java.util.ArrayList;
import java.util.List;
public class leetcode118 {
public static List<List<Integer>> generate(int numRows) {
List<List<Integer>> triangle = new ArrayList<>();
if(numRows==0) return triangle;
triangle.add(new ArrayList<>());
triangle.get(0).add(1);
for(int i = 1; i<numRows ;i++){
List<Integer>prevRow = triangle.get(i-1);
List<Integer>currentRow = new ArrayList<>();
currentRow.add(1);
for(int j = 1; j<i ;j++){
currentRow.add(prevRow.get(j-1)+ prevRow.get(j));
}
currentRow.add(1);
triangle.add(currentRow);
}
return triangle;
}
public static void main(String[] args) {
int numRows = 10;
System.out.println(generate(numRows));
}
}