Question:
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
public class Solution {
public int climbStairs(int n) {
if (n == 0 || n == 1) return 1;
if (n < 0) return 0;
return climbStairs(n - 1) + climbStairs(n - 2);
}
}
From the code above, it is not hard to observe that this problem is almost identical to the Fibonacci Problem, if n is large, it takes a long time to find out the result. Therefore, we need to use DP to solve this problem.
public class Solution {
public int climbStairs(int n) {
if (n == 0 || n == 1) return 1;
int prev = 1;
int current = 1;
for (int i = 2; i <= n; i++) {
int temp = current + prev;
prev = current;
current = temp;
}
return current;
}
}
本文探讨了经典的爬楼梯问题,给出了两种不同的解决方案:递归方法和动态规划方法。通过对比,强调了动态规划方法在解决此类问题时的高效性和实用性。
492

被折叠的 条评论
为什么被折叠?



