[LeetCode] Pascal's Triangle II

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?

解题思路:

题目要求空间复杂度为O(k),我们只需记录上一层的记录即可。这里的k从0开始计数。

class Solution {
public:
    vector getRow(int rowIndex) {
        if(rowIndex<0){
            return vector();
        }
        vector result;
        result.push_back(1);
        for(int i = 1; i< rowIndex + 1; i++){       //第i行
            vector lastRow = result;
            result.resize(i+1);
            for(int j=0; j


你可能感兴趣的:(c++,oj)