Skip to content

Instantly share code, notes, and snippets.

@autekroy
Created September 5, 2014 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save autekroy/b90b22a0438c0077f26b to your computer and use it in GitHub Desktop.
Save autekroy/b90b22a0438c0077f26b to your computer and use it in GitHub Desktop.
LeetCode OJ - Pascal's Triangle
class Solution {
public:
vector<vector<int> > generate(int numRows) {
vector<vector<int>> v;
vector<int> n, next;
if(numRows < 1) return v;
if(numRows >= 1){
n.push_back(1);
v.push_back(n);
}
if(numRows >= 2){
n.push_back(1);//[1, 1]
v.push_back(n);
}
if(numRows > 2){
for(int i = 3; i <= numRows; i++){
next.push_back(1);
for(int j = 1; j <= i -2; j++)
next.push_back(n[j-1] + n[j]);
next.push_back(1);
n = next;
next.clear();
v.push_back(n);
}
}
return v;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment