lurenaa的博客

🚌递归实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<vector<int> > vec;
for(int i = 0; i <= rowIndex; ++i)
{
vec.push_back({});
for(int j = 0; j <= i; ++j)
{
vec[i].push_back(helper(i, j, vec));
}
}
return vec[rowIndex];
}
int helper(int i, int j, vector<vector<int >>& vec)
{
if(j == 0 || i == j)
return 1;
return vec[i - 1][j - 1] + vec[i - 1][j];
}
};

Accepted

34/34 cases passed (0 ms)

Your runtime beats 100 % of cpp submissions

Your memory usage beats 5.16 % of cpp submissions (9 MB)