[C++]LeetCode: 52 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?

思路解析:

无法一下子判断是 Fibonacci number,于是开始分析问题。

the number of solutions for N steps stair(S[N]),可以由两部分组成,S[N-1] && S[N-2],由于剩下一步或者两步可以一次完成(题目要求)。则有S[N]
= S[N-1] + S[N-2]。 由此联想到斐波那契数列

数学上,费波那契数列是以递归的方法来定义:

  • (n≧2)

用文字来说,就是费波那契数列由0和1开始,之后的费波那契系数就由之前的两数相加。首几个费波那契系数是(?A000045):

01123581321345589144233……

复杂度:O(N)

Attention:

1.注意循环的次数,N-2次;还有stepOne和stepTwo的初始值。

2.算法的精妙处,空间复杂度为常数,因为只使用了两个变量保存前两项的值,而不是用容器vector保存数列的全部值。

 
    ret = stepOne + stepTwo;    //S[N] = S[N-1] + S[N-2]

stepTwo = stepOne;          //S[N-1]_new = S[N-2]_old

stepOne = ret;              //S[N-2]_new = S[N]_old

AC Code:

class Solution {
public:
    int climbStairs(int n) {
        if(n == 0 || n == 1) return 1;
        //Fibonacci number 初始化
        int stepOne = 1, stepTwo = 1;
        int ret = 0;

        //Fibonacci number S[N] = S[N-1] + S[N-2]. stepOne计为S[N-1], stepTwo计为S[N-2],从第0项开始。统计N-2次。
        for(int i = 2; i <= n; i++)
        {
            ret = stepOne + stepTwo;    //S[N] = S[N-1] + S[N-2]
            stepTwo = stepOne;          //S[N-1]_new = S[N-2]_old
            stepOne = ret;              //S[N-2]_new = S[N]_old
        }

        return ret;
    }
};

时间: 2024-10-04 07:10:33

[C++]LeetCode: 52 Climbing Stairs的相关文章

[LeetCode OJ]-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? 一共有n阶楼梯,每次只能爬1阶或2阶,问爬到顶端一共有多少种方法 方法一: 利用二叉树的思想,对于n=3时有3种方法,有几个叶子节点便有几种方法 1 void clim

【LeetCode】Climbing Stairs

Set集合的配置 数据表的创建:表关系一个员工拥有多个身份 create table EMPLOYEE ( id INT NOT NULL auto_increment, first_name VARCHAR(20) default NULL, last_name VARCHAR(20) default NULL, salary INT default NULL, PRIMARY KEY (id) ); create table CERTIFICATE ( id INT NOT NULL aut

Leetcode 动态规划 Climbing Stairs

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Climbing Stairs Total Accepted: 13319 Total Submissions: 40778 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 distinc

LeetCode | 0070. Climbing Stairs爬楼梯【Python】

LeetCode 0070. Climbing Stairs爬楼梯[Easy][Python][动态规划] Problem 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? Note: Given n

[LeetCode][JavaScript]Climbing Stairs

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? https://leetcode.com/problems/climbing-stairs/ 直接递归超时了,要动态规划. 打印前几个数

leetCode 70. Climbing Stairs | 动态规划

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? 思考: top steps 1 1 2 2 3 3 4 5 5 8 6 13 ... ... 从上面的分析可以看出,f(top)

leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

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? 思路:题目也比较简单,类似斐波那契. 代码如下: public class Solution { public int climbSta

【LeetCode】Climbing Stairs (2 solutions)

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? 这是最简单的DP了,递推公式为f(n)=f(n-1)+f(n-2) 因此建立vector存放中间结果即可. class Solution

[leetcode] 14. Climbing Stairs

这道题leetcode上面写着是DP问题,问题是我一开始写了个简单的递归结果直接超时,所以没办法只好拿迭代来做了.题目如下: 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? 这个基本就可以很简单的做成递归.如果楼层是