-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path118.java
More file actions
40 lines (39 loc) · 1.03 KB
/
118.java
File metadata and controls
40 lines (39 loc) · 1.03 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
// Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
// Example:
// Input: 5
// Output:
// [
// [1],
// [1,1],
// [1,2,1],
// [1,3,3,1],
// [1,4,6,4,1]
// ]
// Runtime: 0 ms, faster than 100.00% of Java online submissions for Pascal's Triangle.
// Memory Usage: 33.7 MB, less than 7.23% of Java online submissions for Pascal's Triangle.
class Solution {
public List<List<Integer>> generate(int numRows) {
List<Integer> prev = new ArrayList<>(1);
prev.add(new Integer(1));
List<List<Integer>> ans = new ArrayList<List<Integer>>(numRows);
if (numRows == 0) {
return ans;
}
ans.add(prev);
if (numRows == 1) {
return ans;
}
for (int i = 2; i <= numRows; i++) {
List<Integer> cur = new ArrayList<>();
int last = prev.size() - 1;
cur.add(prev.get(0));
for (int j = 0; j < last; j++) {
cur.add(prev.get(j) + prev.get(j+1));
}
cur.add(prev.get(last));
ans.add(cur);
prev = cur;
}
return ans;
}
}