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

    119. 杨辉三角 II - 图1

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

    示例:

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


    注意:

    此处与杨辉三角第一题的起始下标位置不同,此题从0开始,需要注意下标越界的问题

    1. class Solution {
    2. public List<Integer> getRow(int rowIndex) {
    3. List<List<Integer>> result = new ArrayList<>();
    4. for (int i = 0; i <= rowIndex; i++) {
    5. List<Integer> row = new ArrayList<>();
    6. for (int j = 0; j <= i; j++) {
    7. if (j == 0 || j == i){
    8. row.add(1);
    9. }else {
    10. row.add(result.get(i - 1).get(j - 1) + result.get(i - 1).get(j));
    11. }
    12. }
    13. result.add(row);
    14. }
    15. return result.get(rowIndex);
    16. }
    17. }