LeetCode 118 Pascal's Triangle

    xiaoxiao2021-04-19  147

    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] ] AC代码如下:

    class Solution { public: vector<vector<int>> generate(int numRows) {//每一行的最左、最右都为1,中间的值都为上面一行对应两个值的和 vector<vector<int>> ret; for(int i=0;i<numRows;++i){ vector<int> temp; for(int j=0;j<=i;++j){ if(j==0||j==i) temp.push_back(1); else temp.push_back(ret[i-1][j-1]+ret[i-1][j]); } ret.push_back(temp); } return ret; } };

    转载请注明原文地址: https://ju.6miu.com/read-676171.html

    最新回复(0)