My C++ code, 0ms


  • 18
    D

    just do it in a straightforward, each time generate a new line, first push all 1 vector into res and then update the elements from 1 to i-1.

    class Solution {
    public:
        vector<vector<int>> generate(int numRows) {
            vector<vector<int>> res;
            for(auto i=0;i<numRows;++i)
            {
                res.push_back(vector<int>(i+1,1));
                for(auto j=1; j<i; ++j) res[i][j] = res[i-1][j-1] + res[i-1][j];
            }
            return res;
        }
    };

  • 0
    I

    How to understand the very right and very left numbers (1) for each row, since there will be no res[i-1][j-1], but only res[i-1][j]?


  • 0
    B

    @ipanema1, he filled the vector initially with 1s


  • 0
    M

    your code has something wrong...
    if numRows<0, your solution is [ ], but system will gave you [[1]].
    Of course I know numRows must bigger than 0, but numRows is int, not unsigned int.


  • 1
    J

    It's straightfoward and neat. However I submit several times, and none of their runtime is 0ms, almost 3ms.


  • 0

    @job_sucks me,too. and my code run the same time, 3ms every time.


Log in to reply
 

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