Problem Link: Click
This is a simple pascal triangle problem. One matter we have to keep in mind is we may have resize the vector size and allocate them carefully. Hence we have to return the vector.
Code:
class Solution { public: vector<vector<int>> generate(int numRows) { vector<vector<int> > vec(numRows); for(int i=0;i<numRows;i++){ vec[i].resize(i+1); vec[i][0]=1; vec[i][i]=1; } for(int i=2;i<numRows;i++){ for(int j=1;j<i;j++){ vec[i][j]=vec[i-1][j]+vec[i-1][j-1]; } } return vec; } };
No comments:
Post a Comment
If you have any doubts, let me know through comments