1957

无聊蛋疼的1957写的低端博客
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

[leetcode]Pascal's Triangle

Posted on 2013-10-31 10:56  1957  阅读(133)  评论(0编辑  收藏  举报
class Solution {
public:
    vector<vector<int> > generate(int numRows) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        vector<vector<int> > ans;
        if(numRows == 0) return ans;
        vector<int> tmp ; tmp.push_back(1);
        ans.push_back(tmp);
        for(int i = 1 ; i < numRows ; i++){
          //  cout << i << endl;
            int element = i + 1;
            vector<int> x(element);
            x[0] = 1 ; x[element-1] = 1;
            for(int j = 1 ; j < element - 1 ; j++)
                x[j] = ans[i-1][j-1] + ans[i-1][j];
            ans.push_back(x);
        }
        return ans;
    }
};