119 Pascal's Triangle II

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

示意图

Example:

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

Note:

Note that the row index starts from 0.

解释下题目:

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

1. 跟118一样的解法呗

实际耗时:1ms

public List<Integer> getRow(int rowIndex) {
        List<Integer> 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)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容