-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray119.java
More file actions
45 lines (42 loc) · 1.06 KB
/
Array119.java
File metadata and controls
45 lines (42 loc) · 1.06 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
package array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* @ProjectName: leetcode
* @Package: array
* @ClassName: Array119
* @Author: markey
* @Description:119. 杨辉三角 II
* 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。
*
*
*
* 在杨辉三角中,每个数是它左上方和右上方的数的和。
*
* 示例:
*
* 输入: 3
* 输出: [1,3,3,1]
* 进阶:
*
* 你可以优化你的算法到 O(k) 空间复杂度吗?
* @Date: 2020/4/4 12:00
* @Version: 1.0
*/
public class Array119 {
public List<Integer> getRow(int rowIndex) {
int[] result = new int[rowIndex];
for (int i = 0; i < rowIndex; i++) {
for (int j = i; j >= 0; j--) {
if (j == i || j == 0) {
result[j] = 1;
} else {
result[j] = result[j] + result[j-1];
}
}
}
return Arrays.stream(result).boxed().collect(Collectors.toList());
}
}