美文网首页程序员
Leetcode - Pascal's Triangle II

Leetcode - Pascal's Triangle II

作者: 哈比猪 | 来源:发表于2016-09-27 19:09 被阅读0次

题目链接

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?

解题思路

TODO (稍后补充)

解题代码

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int> op(rowIndex+1, 0);
        op[0]=1;
        for (int i=1;i<=rowIndex;i++) {
            for (int k=i;k>=1;k--) {
               op[k] = op[k] + op[k-1]; 
            }
            
        }
        return op;
        
    }
};

相关文章

网友评论

    本文标题:Leetcode - Pascal's Triangle II

    本文链接:https://www.haomeiwen.com/subject/vjxhyttx.html