[LeetCode]Pascal's Triangle

Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5,
Return

[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

思考:边界单独考虑。

class Solution {
private:
    vector<vector<int> > res;
    vector<int> ans;
public:
    vector<vector<int> > generate(int numRows) {
        if(numRows==0) return res;
        int row=0;
        while(row<numRows)
        {
            ans.clear();
            for(int i=0;i<=row;i++)
            {
                if(i==0||i==row) ans.push_back(1);
                else ans.push_back(res[row-1][i-1]+res[row-1][i]);
            }
            row++;
            res.push_back(ans);
        }
        return res;
    }
};

  

posted @ 2014-01-16 16:33  七年之后  阅读(174)  评论(0编辑  收藏  举报