@requires_authorization
@author johnsondu
@create_time 2015.7.23 19:54
@url [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)
/************************
* @description: simple.
* @time_complexity: O(n)
* @space_complexity: O(n)
************************/
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> ans;
for(int i = 1; i <= numRows; i ++) {
vector<int> layer;
if(i == 1) layer.push_back(1);
else {
for(int j = 1; j <= i; j ++) {
if(j == 1 || j == i) layer.push_back(1);
else layer.push_back(ans[i-2][j-2] + ans[i-2][j-1]);
}
}
ans.push_back(layer);
}
return ans;
}
};
@requires_authorization
@author johnsondu
@create_time 2015.7.23 19:54
@url [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)
/************************
* @description: simple.
* @time_complexity: O(n)
* @space_complexity: O(n)
************************/
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> ans;
if(numRows < 1) return ans;
vector<int> first;
first.push_back(1);
ans.push_back(first);
if(numRows < 2) return ans;
vector<int> second;
second.push_back(1);
second.push_back(1);
ans.push_back(second);
for(int i = 3; i <= numRows; i ++) {
vector<int> layer;
layer.push_back(1);
for(int j = 0; j < ans[i-2].size()-1; j ++) {
layer.push_back(ans[i-2][j] + ans[i-2][j+1]);
}
layer.push_back(1);
ans.push_back(layer);
}
return ans;
}
};