美文网首页
Leetcode - Pascal's Triangle

Leetcode - Pascal's Triangle

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

题目链接

Pascal's Triangle

Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return as:

图 1-1 题目

解题思路

TODO (稍后补充)

解答代码

class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        vector<vector<int> > op;
        if (numRows == 0) return op;
        vector<int> line(numRows, 0);
        line[0]=1;
        op.push_back(vector<int>(1,1));
        for (int i=1;i<numRows;i++) {
            for (int k=i;k>=1;k--) {
               line[k] = line[k] + line[k-1]; 
            }
            op.push_back(vector<int>(line.begin(), line.begin()+i+1));
        }
        return op;
    }
};

相关文章

网友评论

      本文标题:Leetcode - Pascal's Triangle

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