[leetcode] 70. Climbing Stairs

原创 2016年06月02日 12:16:29

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?

Solution 1

idea: recursive function, however, run out of limit time

class Solution {
public:
    int climbStairs(int n) {
        if (n==1)
            return 1;
        else if (n==2)
            return 2;
        else
            return climbStairs(n-1)+climbStairs(n-2);
    }
};

Solution 2

idea: similar to Fibonacci number

class Solution {
public:
    int climbStairs(int n) {
        if (n==1)
            return 1;
        else if (n==2)
            return 2;
        else{
            int ppre = 1;
            int pre = 2;
            for (int i = 3; i < n; i++){
                int tmp = pre;
                pre = ppre+pre;
                ppre = tmp;
            }
            return pre+ppre;
        }
        
    }
};



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

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 70 Climbing Stairs(爬楼梯)(动态规划)(*)

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

[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(C++解法 动态规划思想)

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

LeetCode【70】Climbing Stairs

根据题目找规律,见下表策略选择          分析:斐波那契数列的变形           1 1 2 3 5 8 ……        ...

LeetCode70. 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】No.70 Climbing Stairs

【原题】 Given an array of integers, find two numbers such that they add up to a specific target number...

Leetcode刷题记——70. Climbing Stairs(爬楼梯)

一、题目叙述: You are climbing a stair case. It takes n steps to reach to the top. Each time...

Leetcode_70_Climbing Stairs

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41851705 You are climbing a st...

leetcode解题之70 #Climbing Stairs Java版 (青蛙跳台阶)

leetcode解题之70 #Climbing Stairs Java版 (青蛙跳台阶)
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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