pascals-triangle-ii

1627 단어 LeetCode
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;
    }
};

좋은 웹페이지 즐겨찾기