C++ solution


  • 0
    R
    public:
        vector<vector<int>> generate(int numRows) {
            vector<vector<int>> res;
            vector<int> temp2;
            vector<int> temp;
            temp.push_back(1);
            temp2.push_back(1);
            for(int i = 0; i < numRows; ++i){
                res.push_back(temp);
                temp2 = temp;
                temp.clear();
                temp.push_back(1);
                for(int j = 0; j < temp2.size() - 1; ++j){
                     temp.push_back(temp2[j] + temp2[j+1]);
                }
                temp.push_back(1);
            }
            return res;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.