[LeetCode118]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 {
public:
    vector<vector<int>> generate(int numRows) {
        vector<vector<int>> vec1;
        for(int i = 0; i < numRows; ++i)
        {
            vector<int> vec2;
            vec2.push_back(1);
            for(int j = 1; j < i; ++j)
            {
                int tmp = vec1[i-1][j-1] + vec1[i-1][j];
                vec2.push_back(tmp);
            }
            if(i != 0)
                vec2.push_back(1);
            vec1.push_back(vec2);
        }
        return vec1;
    }
};
原文地址:https://www.cnblogs.com/zhangbaochong/p/5180070.html