-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalTriangleII.java
More file actions
51 lines (48 loc) · 1.01 KB
/
PascalTriangleII.java
File metadata and controls
51 lines (48 loc) · 1.01 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
package Lc119_PascalTriangleII;
import java.util.ArrayList;
import java.util.List;
/**
* 119. Pascal's Triangle II
*
* Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle.
*
* In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
*
* Example 1:
*
* Input: rowIndex = 3
* Output: [1,3,3,1]
*
* Example 2:
*
* Input: rowIndex = 0
* Output: [1]
*
* Example 3:
*
* Input: rowIndex = 1
* Output: [1,1]
*
*
* Constraints:
*
* 0 <= rowIndex <= 33
*/
public class PascalTriangleII {
/**
* Solution 1
* Time complexity : O(rowIndex^2)
* Space complexity : O(1)
*/
public List<Integer> getRow(int rowIndex) {
List<Integer> ans = new ArrayList<>(rowIndex + 1);
ans.add(1);
for (int i = 1; i <= rowIndex; i++) {
ans.add(0);
for (int j = i; j > 0; j--) {
ans.set(j, ans.get(j) + ans.get(j - 1));
}
}
return ans;
}
}