题目:
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,是由从n-1台阶走一步来到或者从n-2台阶走两步来到
package dp; public class ClimbingStairs { public int climbStairs(int n) { int[] dp = new int[n + 1]; dp[0] = dp[1] = 1; for (int i = 2; i <= n; ++i) { dp[i] = dp[i - 1] + dp[i - 2]; } return dp[n]; } public static void main(String[] args) { // TODO Auto-generated method stub ClimbingStairs c = new ClimbingStairs(); System.out.println(c.climbStairs(4)); } }