【LeetCode】118. 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<int> tmp;
        vector<vector<int>> result,no_result;
        if(numRows==0) return no_result;
        if(numRows==1) {
            tmp.push_back(1);
            result.push_back(tmp);
            return result;
        }
     
        if(numRows>=2)
        {
            result = generate(numRows -1);
            vector<int> pre_result = result[numRows-2];
            for(int i = 0; i<numRows; i++)
            {
                if(i==0||i==numRows-1)
                  tmp.push_back(1);
                 else
                  tmp.push_back(pre_result[i-1]+pre_result[i]);
            }
            result.push_back(tmp);
        }
        return result;
    }
};


你可能感兴趣的:(【LeetCode】118. Pascal's Triangle)