CodeLeet70. Climbing Stairs

原创 2018年04月17日 19:13:16

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 will be a positive integer.

Example 1:

Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps

Example 2:

Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step

 这道题粗看很难解,分类能分到吐血,而且不好循环,但是写几个就能发现规律了。

1->1     2->2   3->3  4->5   5->8

典型的斐波拉契数列。

我一开始代码为

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

但是超时了,因为这种递归会重复计算。

后来用了数组来存储。

class Solution {
    public int climbStairs(int n) {
        if(n==1)
            return 1;
        int[]res=new int[n+1];
        res[1]=1;
        res[2]=2;
        for(int i=3;i<n+1;i++)
        {
            res[i]=res[i-1]+res[i-2];
        }
        return res[n];
    }
}

附斐波拉契数列一种很好的求法。

int Fib2(int n,int a,int b)//a表示第一个数的值,b表示第二个数的值
{
	if(n<=2)
		return b;
	return Fib2(n-1,b,a+b);
}

70. Climbing Stairs [easy] (Python)

题目链接https://leetcode.com/problems/climbing-stairs/题目原文 You are climbing a stair case. It takes n s...
  • coder_orz
  • coder_orz
  • 2016-05-26 13:10:58
  • 1714

LeetCode 70 — Climbing Stairs(C++ Java Python)

题目:http://oj.leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It takes n steps...
  • dragon_dream
  • dragon_dream
  • 2014-03-20 22:17:03
  • 3045

LeetCode Climbing Stairs 递归求解和动态规划法

简单题目,相当于fibonacci数列问题,难点就是要会思维转换,转换成为递归求解问题,多训练就可以了。 所以这种类型的题目相对于没有形成递归逻辑思维的人来说,应该算是难题。 我的想法是: 每次有两种...
  • kenden23
  • kenden23
  • 2013-12-21 08:13:57
  • 17702

leetcode 70 Climbing Stairs

Climbing Stairs                       You are climbing a stair case. It takes n steps to reach t...
  • wangyaninglm
  • wangyaninglm
  • 2015-04-29 17:46:09
  • 2859

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...
  • haimianxiaodao
  • haimianxiaodao
  • 2016-09-21 15:19:49
  • 481

LWC 63:746. Min Cost Climbing Stairs

LWC 63:746. Min Cost Climbing Stairs传送门:746. Min Cost Climbing StairsProblem: On a staircase, the ...
  • u014688145
  • u014688145
  • 2017-12-19 10:19:52
  • 620

LeetCode | 746. Min Cost Climbing Stairs | 简单DP上楼梯问题

746. Min Cost Climbing Stairs My SubmissionsBack to Contest ·       User Accepted:995 ·      ...
  • u012737193
  • u012737193
  • 2017-12-21 19:51:47
  • 168

LeetCode 70 Climbing Stairs(Python详解及实现)

【题目】 You are climbing a stair case. It takes nsteps to reach to the top.   Each time you can eith...
  • yangjingjing9
  • yangjingjing9
  • 2017-08-05 12:56:00
  • 384

Climbing Stairs -- LeetCode

原题链接: http://oj.leetcode.com/problems/climbing-stairs/  这道题目是求跑楼梯的可行解法数量。每一步可以爬一格或者两个楼梯,可以发现,递推式是f(n...
  • linhuanmars
  • linhuanmars
  • 2014-04-18 00:54:46
  • 9903

Climbing Stairs-python

class Solution(object): def climbStairs(self,n): """ :param n: int :ret...
  • wxd_zswx
  • wxd_zswx
  • 2015-12-05 18:46:46
  • 287
收藏助手
不良信息举报
您举报文章:CodeLeet70. Climbing Stairs
举报原因:
原因补充:

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