A c++ colution


  • 0
    vector<vector<int>> generate(int numRows) {
            vector<vector<int>> ans;
            if(!numRows) return ans;
            
            ans.push_back(vector<int>(1,1));
            
            for(int i=1; i<numRows; i++){
                vector<int> v;
                for(int j=0; j<=i; ++j) {
                    if(j==0 || j==i){
                        v.push_back(1);
                    }else{
                        v.push_back(ans[i-1][j-1]+ans[i-1][j]);
                    }
                } 
                ans.push_back(v);
            }
            
            return ans;
        }
    

Log in to reply
 

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