LeetCode题解:Pascal's Triangle II

Given an index k, return the kth row of the Pascal’s triangle.

For example, given k = 3,
Return [1,3,3,1].

Note:
Could you optimize your algorithm to use only O(k) extra space?

题意:返回给定k对应第k行的 Pascal 三角形

解决思路:其实很简单,把 Pascal 三角形的解决办法改一改就可以了

代码:

public List<Integer> getRow(int rowIndex) {
        List<Integer> row = new ArrayList<Integer>();

        for(int i = 0;i < rowIndex + 1;++i){
            row.add(0,1);

            for(int j = 1;j < row.size() - 1;++j){
                row.set(j, row.get(j) + row.get(j + 1));
            }
        }

        return row;
    }

你可能感兴趣的:(LeetCode)