Created
September 5, 2014 07:25
-
-
Save autekroy/b90b22a0438c0077f26b to your computer and use it in GitHub Desktop.
LeetCode OJ - Pascal's Triangle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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