pascals-triangle-ii

Given an index k, return the k th 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?

思路:递归调用

代码:

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int> res;
        res.push_back(1);
        if(rowIndex==0)//开始写成了==0返回空,==1返回一个数字的容器,又给自己挖一个大坑
            return res;
        vector<int> last = getRow(rowIndex-1);
        res.resize(last.size()+1);
        res[0] = 1;
        res[rowIndex] = 1;
        for(int i = 1;i < rowIndex;++i){
            res[i] = last[i-1]+last[i];
        }
        return res;
    }
};

你可能感兴趣的:(LeetCode)