LeetCode Algorithms #66 <Pascal's Triangle>

来源:互联网 发布:如何打开oracle数据库 编辑:程序博客网 时间:2024/06/18 15:45

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>> result;        for(int rowIndex = 0; rowIndex < numRows; rowIndex++)        {            vector<int> rowVector;            for(int idx = 0; idx < rowIndex+1; idx++)            {                if(idx == 0 || idx == rowIndex)                {                    rowVector.push_back(1);                    continue;                }                                rowVector.push_back(result[rowIndex-1][idx-1] + result[rowIndex-1][idx]);            }            result.push_back(rowVector);        }        return result;    }        };


0 0