26.杨辉三角 II

/*给定一个非负索引 rowIndex,返回「杨辉三角」的第 rowIndex 行。

在「杨辉三角」中,每个数是它左上方和右上方的数的和。
class Solution {
    public List<Integer> getRow(int rowIndex) {
        int[] dp = new int[rowIndex + 1];
        List<Integer> ret = new ArrayList<>(rowIndex + 1);
        dp[0] = 1;
        if (rowIndex == 0) {
            return Arrays.stream(dp).boxed().collect(Collectors.toList());
        }
        dp[1] = 1;
        for (int i = 2; i <= rowIndex; i ++) {
            dp[i] = 1;
            for (int j = i - 1; j >= 1; j --) {
                dp[j] = dp[j] + dp[j - 1];
            }
        }
        return Arrays.stream(dp).boxed().collect(Collectors.toList());
    }
}

 

posted @ 2022-03-12 08:42  随遇而安==  阅读(15)  评论(0编辑  收藏  举报