[Leetcode] Pascal's Triangle

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;
    }
};

你可能感兴趣的:([Leetcode] Pascal's Triangle)