My C++ code, 0ms

  • 18

    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 {
        vector<vector<int>> generate(int numRows) {
            vector<vector<int>> res;
            for(auto i=0;i<numRows;++i)
                for(auto j=1; j<i; ++j) res[i][j] = res[i-1][j-1] + res[i-1][j];
            return res;

  • 0

    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

    @ipanema1, he filled the vector initially with 1s

  • 0

    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

    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.