leetcode--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?
public class Solution {
/**This problem is a simple application of DP.<br>
* ways[n] = ways[n - 1] + ways[n - 2].<br>
* @author Averill Zheng
* @version 2014-06-04
* @since JDK 1.7
*
*/
public int climbStairs(int n) {
int differentWays = 0;
if(n == 1)
differentWays = 1;
if(n == 2)
differentWays = 2;
if(n > 2){
int[] ways = new int[n];
ways[0] = 1;
ways[1] = 2;
for(int i = 2; i < n; ++i)
ways[i] = ways[i - 1] + ways[i - 2];
differentWays = ways[n-1];
}
return differentWays;
}
}

浙公网安备 33010602011771号