LeetCode_Pascal's Triangle

2926 ワード

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;

        if(numRows <= 0) return result;

        

        vector<int> first;

        first.push_back(1); result.push_back(first);

        

        for(int i = 1; i < numRows ; i++)

        {

            vector<int> temp(i+1);

            for(int j = 0 ; j < i+1 ; j++){

                if( j == 0 || j == i){

                    temp[j] = 1;continue;

                }else{

                    temp[j] = result[i-1][j] + result[i-1][j-1] ;

                    continue;

                }

            }

            result.push_back(temp);

        }

        

        return result ;

    }

};