Sunday, November 30, 2014

Pascal's Triangle

easy.  
 vector<vector<int> > generate(int numRows) {
       vector<vector<int> > result;
        if (numRows < 1) return result;
        result.push_back(vector<int>(1,1));//first row (result[0])
        result.resize(numRows);
        for(int ii = 1; ii < numRows; ++ii){
            result[ii].resize(ii+1);
            result[ii][0] = 1;
            result[ii][ii] = 1;
            for(int jj = 1; jj < ii; ++jj){
                result[ii][jj] = result[ii-1][jj-1] + result[ii-1][jj];
            }
        }
        return result;
    }

No comments:

Post a Comment