C++ solution 3ms DP easy read


  • 0
    A
    vector<vector<int> > generate(int numRows) {
        vector<vector<int> > res;
        
        if (numRows == 0)
            return res;
            
        res.push_back({1});
        if (numRows == 1)
            return res;
            
        vector<int> t;
        for(int rows = 1;rows<numRows;rows++) {
            int pSize = res[rows-1].size();
            t.clear();
            t.push_back(1);
            
            for (int i = 1;i<pSize;i++) {
                t.push_back(res[rows-1][i-1]+res[rows-1][i]);
            }
            
            t.push_back(1);
            res.push_back(t);
        }
        return res;
    }

Log in to reply
 

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