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?
题目意思 : n步到达,每次可以走一步或两步 , 有多少种走法!
题目思路 : 当你最后一步走 一步的时候,对应着的 == W(n - 1) , 当你走两步 , == W(n - 2);
W(n) = W(n - 1) + W(n - 2)
子问题就是 当n 分别等于 1 , 2 , 3, 4 , ...... n - 1时对应的方法数。
public class Solution {
public int climbStairs(int n) {
int[] ways = new int[n];
if (n <= 0) return 0;
ways[0] = 1;
if (n >= 2) ways[1] = 2;
else return ways[0];
for (int i = 2; i < n ; i++) {
ways[i] = ways[i - 1] + ways[i - 2];
}
return ways[n - 1];
}
}