Leetcode - Pascal's Triangle

2016-09-27  本文已影响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;
    }
};
上一篇 下一篇

猜你喜欢

热点阅读