首页 > 其他 > 详细

Climbing Stairs

时间:2017-05-25 09:30:40      阅读:318      评论:0      收藏:0      [点我收藏+]
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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!