119. Pascal's Triangle II

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ret{1};
for(int i=1;i<=rowIndex ; ++i){
int pre = ret[0];
for(int j=1;j<i ; ++j){
int cur = ret[j];
ret[j] += pre;
pre = cur;
}
ret.push_back(1);
}
return ret;
}
};

analysis

  • time complexity O(n)
  • space complexity O(n)