-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay11.java
More file actions
30 lines (24 loc) · 812 Bytes
/
Copy pathDay11.java
File metadata and controls
30 lines (24 loc) · 812 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
//Problem:Pascal's Triangle
//https://leetcode.com/problems/pascals-triangle/
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<>();
if (numRows == 0) {
return result;
}
List<Integer> firstRow = new ArrayList<>();
firstRow.add(1);
result.add(firstRow);
for (int i = 1; i < numRows; i++) {
List<Integer> prevRow = result.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);
result.add(currentRow);
}
return result;
}
}