Climbing Stairs-LeetCode

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?

 

DP解法,类似斐波那契数列

 1 public class Solution {
 2     public int climbStairs(int n) {
 3         int[] res=new int[n+1];
 4         res[0]=1;
 5         res[1]=1;
 6         
 7         for(int i=2; i<=n; i++){
 8             res[i]=res[i-1]+res[i-2];
 9         }
10         return res[n];
11     }
12 }

 

posted on 2014-04-15 04:36  iisahu  阅读(113)  评论(0编辑  收藏  举报

导航