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?

---

map[n+1]!!!

(0-n), length = n+1

public class Solution {
    
    public int climbStairs(int n) {
        
       int[] map = new int[n+1];
       
       // init
       for(int i=0; i<map.length; i++){
            map[i] = -1;
       }
       return helper(n, map);
        
    }
    
    
    // DP with cache
    private int helper(int n, int[] map){
        if(n<0)         return 0;
        else if(n==0)    return 1;
        else if(map[n] > -1)    return map[n];
        else{
            map[n] = climbStairs(n-1) + climbStairs(n-2);
            return map[n];
        }
    }
}

 

posted @ 2013-09-11 10:40  LEDYC  阅读(127)  评论(0)    收藏  举报