LeetCode 119 杨辉三角II
LeetCode 119 杨辉三角II
原地动态规划
class Solution {
//动态规划,由第k-1行递推第k行
public List<Integer> getRow(int rowIndex) {
//第k行共有k个元素
int k = rowIndex+1;
int[] kThRow = new int[k];
kThRow[0] = 1;
//递推
for(int row=2; row<=k; row++) {
//从后往前计算,原地
kThRow[row-1] = 1;
for(int idx=row-2; idx>0; idx--) {
kThRow[idx] = kThRow[idx-1] + kThRow[idx];
}
}
return Arrays.stream(kThRow).boxed().collect(Collectors.toList());
}
}