70. Climbing Stairs
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?
Note: Given n will be a positive integer.
题目含义:n层高的梯子,每一步能爬1层或2层,问从底爬到顶端有多少种爬法
1 public int climbStairs(int n) { 2 if (n==1) return 1; 3 else if (n==2) return 2; 4 // return climbStairs(n-1) + climbStairs(n-2); //递归方式会超时 5 int[] dp = new int[n]; 6 dp[0] = 1; dp[1] = 2; 7 for (int i = 2; i < n; ++i) { 8 dp[i] = dp[i - 1] + dp[i - 2]; 9 } 10 return dp[n - 1]; 11 }