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) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<vector<int>> result;
        
        vector<int> tmp;
        for(int i = 0; i < numRows; i++){
            tmp.push_back(1);
            result.push_back(tmp);
        }
        
        for(int i = 2; i < numRows; i++){
            for(int j = 1; j < i; j++){
                result[i][j] = result[i-1][j-1] + result[i-1][j];
            }
        }
        
        return result;
    }
};



你可能感兴趣的:(LeetCode)