杨辉三角 II

2023-05-13,

题目描述

给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。

在杨辉三角中,每个数是它左上方和右上方的数的和。

示例:

输入: 3
输出: [1,3,3,1]

贴出代码

class Solution {
    public List<Integer> getRow(int rowIndex) {
        List<Integer> list = new ArrayList<>();
        if(rowIndex < 0)
            return list;
        list.add(1);
        if(rowIndex == 0)
            return list;
        for(int i = 1;i<=rowIndex;i++) {
            for(int j = list.size()-1;j>0;j--) {
                list.set(j, list.get(j-1)+list.get(j));
            }
            list.add(1);
        }
        return list;
    }
}

杨辉三角 II的相关教程结束。

《杨辉三角 II.doc》

下载本文的Word格式文档,以方便收藏与打印。