码迷,mamicode.com
首页 > 其他好文 > 详细

Climbing Stairs

时间:2017-05-25 10:03:35      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:eps   air   div   i++   code   color   ret   distinct   int   

public class Solution {
    /**
     * @param n: An integer
     * @return: An integer
     */
    public int climbStairs(int n) {
        // write your code here
        if (n == 0 || n == 1) {
            return 1;
        }
        
        //f[x] distinct ways num from step 1 to step x
        // because step x equal to step x - 1 ways + making one more step or step x - 2 ways + making two steps
        //f[x] = f[x - 1] + f[x - 2]
        int[] f = new int[n + 1]; 
        f[0] = 1;
        f[1] = 1;
        
        for (int i = 2; i < n + 1; i++) {
            f[i] = f[i - 1] + f[i - 2];
        }
        
        return f[n];
        
    }
}

 

Climbing Stairs

标签:eps   air   div   i++   code   color   ret   distinct   int   

原文地址:http://www.cnblogs.com/codingEskimo/p/6901844.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!