老师麻烦看一下这样写对吗?研究了好久才理解 是不是 太笨了 哈哈
/**
*递推
*/
public int solution(int n) {
int first = 0, second = 1, result = 0;
for (int i = 2; i <= n; ++i) {
result = first + second;
first = second;
second = result;
}
return result;
}
/**
*递归
*/
public int solutionTwo(int n){
if(n == 0)
return 0;
if(n == 1)
return 1;
return solutionTwo(n-1)+solutionTwo(n-2);
}
2020-08-08
1
Tiger
为什么看着看着就卡了?
2023-02-01
王晓聪
久闻动态规划大名
2021-04-15
风清扬
c++递归+记忆化版本AC code:
```
class Solution {
private:
int climbStairs_helper(int n,vector<int> &res)
{
if (n == 1 || n ==2)
res[n] = n;
else if (res[n] == 0)
res[n] = climbStairs_helper(n-1,res) + climbStairs_helper(n-2,res);
return res[n];
}
public:
int climbStairs(int n) {
vector<int> res(n + 1,0);
climbStairs_helper(n,res);
return res[n];
}
};
```