Only one vector needed. 5ms C++ solution


  • 0
    A
    class Solution {
    public:
        vector<vector<int> > generate(int numRows) {
            vector<vector<int> > result;                
            vector<int> row;
            for (int i = 0; i < numRows; i++) {
                row.resize(i+1, 1);
                for (int j = row.size()-2; j > 0; j--)
                    row[j] = row[j] + row[j-1];
                
                result.push_back(row);
            }
            
            return result;
        }
    };

Log in to reply
 

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