leetcode 70 Climbing Stairs

简介:  Climbing Stairs                       You are climbing a stair case. It takes n steps to reach to the top.

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?

Hide Tags



这个题面试题还是比较常见的
讨论的帖子:

原理:

This problem is a Fibonacci problem. F(n)=F(n-1)+F(n-2); Solving this problem by recursion ,we will do a lot of same recursion. Example: F(10)=F(9)+F(8); F(9)=F(8)+F(7); we calculate F(8) twice,when n is large,this will increase as a rate of n's exponent.

So a more efficient way to solve this problem is from Bottom to Top. Calculate F(0) ,F(1); then F(2).........

人生ac最快的代码:
class Solution {
public:
    int climbStairs(int n)
    {
        
        int stepone = 0;
        int steptwo = 1;
        int sum = 0;
        
        for(int i = 0;i<n;i++)
        {
            sum = stepone + steptwo;
            stepone = steptwo;
            steptwo = sum;
            
            
        }
        
        return sum;
    }
};





DP算法求解:

简洁的代码:

递归:

python:


相关文章
LeetCode 70. Climbing Stairs
你正在爬楼梯。 它需要n步才能达到顶峰。 每次你可以爬1或2步。 您可以通过多少不同的方式登顶? 注意:给定n将是一个正整数。
52 0
LeetCode 70. Climbing Stairs
LeetCode 70. 爬楼梯 Climbing Stairs
LeetCode 70. 爬楼梯 Climbing Stairs
Leetcode-Easy 70. Climbing Stairs
Leetcode-Easy 70. Climbing Stairs
93 0
Leetcode-Easy 70. Climbing Stairs
|
算法 移动开发
[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 or 2 steps. In how many distinct ways can you climb to the top? 这是一个很经典的爬楼梯问题,面试也会经常遇
1150 0
LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50514606 翻译 你正在爬一个楼梯。
927 0
|
C++ Python
[LeetCode] Climbing Stairs
Note: If you feel unwilling to read the long codes, just take the idea with you. The codes are unnecessarily long due to the inconvenient handle of matrices.
703 0
|
机器学习/深度学习
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
41 6
|
1月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
71 2