119 Pascal's Triangle II

Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.

119 Pascal's Triangle II_第1张图片
示意图

Example:

Input: 3
Output: [1,3,3,1]

Note:

Note that the row index starts from 0.

解释下题目:

输出杨辉三角的某一行,注意行是从0开始的

1. 跟118一样的解法呗

实际耗时:1ms

public List getRow(int rowIndex) {
        List list = new ArrayList<>();
        list.add(0, 1);
        for (int i = 0; i < rowIndex; i++) {
            //第一位一定是1
            list.add(0, 1);
            for (int j = 1; j < list.size() - 1; j++) {
                list.set(j, list.get(j) + list.get(j + 1));
            }
        }
        return list;
    }

  思路是真的没什么好写的......

时间复杂度O(n)
空间复杂度O(1)

你可能感兴趣的:(119 Pascal's Triangle II)