c++ solution 0ms and space O(n)


  • 0
    D
    public:
        vector<vector<int>> generate(int numRows) {
            vector<vector<int>> res;
            if(numRows < 1) return {};
            if(numRows==1) return {{1}};
            res.push_back({1});
            vector<int> ans={1,1};
            res.push_back(ans);
            int count = 2;
            while(count < numRows){
                for(int i = count-1; i>=1; --i)
                    ans[i] = ans[i]+ans[i-1];
                ans.push_back(1);
                res.push_back(ans);
                count++;
            }
            return res;
        }
    };

Log in to reply
 

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