Sharing a concise cpp solution


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

Log in to reply
 

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