Climbing Stairs

原创 2015年07月08日 17:22:31

class Solution {
public:
    int climbStairs(int n) {
        //其实是斐波那契数列的变形问题,f(n) = f(n-1) + f(n-2)
        int last1 = 1, last2 = 1;//f0 = 1, f1 = 1
        int result = 1;
        for(int i = 2;i<=n;i++)
        {
            result = last1 + last2;
            last1 = last2;
            last2 = result;
        }
        
        return result;
    }
};


相关文章推荐

leetcode Climbing Stairs

You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb...

Leetcode 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...

LeetCode 之 Climbing Stairs (方法2)

上一次说了自顶向下,没看的可以回去看看 climbing stairs 方法1(自顶向上) 由于本人是学渣,自顶向下太抽象肿么破。。。。尼玛哥教你一个更抽象的,自底向上,自底向上代码比较简单,基本思想...

Leetcode note--Leetcode 70climbing Stairs

70. Climbing Stairs  My Submissions Total Accepted: 143040Total Submissions: 37227...

Climbing Stairs爬楼梯啊爬楼梯(支持n step)

leetcode原题 You are climbing a stair case. It takes n steps to reach to the top. Each time you ...

[LeetCode]70.Climbing Stairs

【题目】 You are climbing a stair case. It takes n steps to reach to the top. Each time you can eithe...

LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)

翻译你正在爬一个楼梯。它需要n步才能到底顶部。每次你可以爬1步或者2两步。那么你有多少种不同的方法爬到顶部呢?原文You are climbing a stair case. It takes n s...
  • NoMasp
  • NoMasp
  • 2016年01月14日 07:12
  • 4248

【LeetCode-面试算法经典-Java实现】【073-Climbing Stairs(爬楼梯)】

【070-Climbing Stairs(爬楼梯)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  You are climbing a stair case. It ta...

DP (1) -- Range Sum Query - Immutable, House Robber, Climbing Stairs

Range Sum Query - Immutable,House Robber
  • dwt0317
  • dwt0317
  • 2017年01月04日 20:27
  • 94

leetcode之70. Climbing Stairs(C++解法 动态规划思想)

原题: You are climbing a stair case. It takes n steps to reach to the top.Each time you can either cl...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Climbing Stairs
举报原因:
原因补充:

(最多只允许输入30个字)