Simple and neat version of C++


  • 1
    Y
    class Solution {
    public:
        vector<vector<int> > generate(int numRows) {
            vector<vector<int> > result;
            if(numRows < 1) return result;
        
            for(int i = 0; i < numRows; i++){
                vector<int> temp;
                for(int j = 0; j <= i; j++){
                    if(j == 0 || j == i)
                        temp.push_back(1);
                    else
                        temp.push_back(result[i - 1][j - 1] + result[i - 1][j]);
                }
                result.push_back(temp);
            }
            return result;
        }
    };

  • 0
    X

    Nice and elegant solution, good job.


Log in to reply
 

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