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());
    }
}
posted @ 2020-08-13 14:39  CodeSPA  阅读(55)  评论(0编辑  收藏  举报