[Leetcode] Pascal's Triangle

来源:互联网 发布:小米5x拍照怎么样知乎 编辑:程序博客网 时间:2024/04/28 19:09
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> > res;                    if (numRows == 0) return res;                vector<int> temp;        temp.push_back(1);        res.push_back(temp);                if (numRows == 1)            return res;                    temp.clear();        temp.push_back(1);        temp.push_back(1);        res.push_back(temp);        if (numRows == 2)            return res;                int length = 3;        for (int i = 3; i <= numRows; ++i)        {            temp.clear();            temp.push_back(1);            length = i;                        for (int j = 1; j < length - 1; ++j)            {                temp.push_back(res[i - 2][j - 1] + res[i - 2][j]);            }            temp.push_back(1);            res.push_back(temp);        }                return res;    }};