1 解题思想
现在需要怕一个层高为n的楼梯,每次你能爬一楼或两楼,问你有几种方式到顶楼。
动态规划:当你在i楼是,你可以选择从i-1爬一步也可以从i-2爬两步,所以在到位置i的方式是爬到i-1与i-2方式只和,以此类推
2 原题
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?
3 AC解
public class Solution {
/**
* DP 斐波那契数列
* */
public int climbStairs(int n) {
int[] dp=new int[n+1];
dp[0]=1;
dp[1]=1;
for(int i=2;i<=n;i++)
dp[i]=dp[i-1]+dp[i-2];
return dp[n];
}
}