Leetcode_num13_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?

很简单的思路,因为一次可以走1~2步,所以到达第n级可以从第n-1级,也可以从第n-2级。设到达第n级的方法有s(n)种,s(n)=s(n-1)+s(n-2)

一开始准备用递归做,代码如下:

class Solution:
    # @param n, an integer
    # @return an integer
    def climbStairs(self, n):
        if n<=2:
            return n
        else:
            return self.climbStairs(n-1)+self.climbStairs(n-2)

结果在n=35的时候TLE了,这进一步说明递归的算法效率比较低,但从思路上比较简单明了。

于是,转向迭代了,代码如下:

class Solution:
    # @param n, an integer
    # @return an integer
    def climbStairs(self, n):
        if n<=1:
            return n
        else:
            s=[0 for i in range(n)]
            s[0]=1  #到达第1级
            s[1]=2  #到达第2级
            for i in range(2,n):
                s[i]=s[i-1]+s[i-2]
            return s[n-1] #到达第n级

在此引入一个数组s,记录到达第n级的方法,然实际要求的返回值是s[n],数组s中的前n-1项存储值是多余的。

于是进行改进,设s1为走一步到达方法数,s2为走两步到达的方法数。那么到达第n级台阶时,s(n)=s1+s2,其中s1=s(n-1),s2=s(n-2);到达第n+1级台阶时,s(n+1)=s1+s2,其中s1=s(n)=上一步的s1+s2, s2=s(n-1)=上一步的s1,所以只需要记录s1和s2的值,无需记录n个值

class Solution:
    # @param n, an integer
    # @return an integer
    def climbStairs(self, n):
        if n<=1:
            return n
        else:
            s1=1
            s2=1
            for i in range(1,n):
                s=s1+s2
                s2=s1
                s1=s
            return s 

这应该是比较简单的方法了,受教了!

时间: 2024-08-07 21:17:10

Leetcode_num13_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? 题解:这道题属于动态规划的题,类似于斐波那契数列,所以采用非递归的方式来解.当楼梯只有一级时,显然只有一种方法,即f(1

[LeetCode] Climbing Stairs (Sequence DP)

Climbing Stairs https://oj.leetcode.com/problems/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? 这题比较简单,可以使用动态规划来求解

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? 1 class Solution { 2 public: 3 int climbStairs(int n) { 4 vector<int

[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 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 @ Python

原题地址:https://oj.leetcode.com/problems/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? 解题思路: 爬楼梯问题.经典的动态规划问题.每次上

【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(斐波那契数列问题)

一.题目描述 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阶楼梯共有几种爬法-_-||.题目可以看成是,设f(n)表示爬到第n 阶楼梯的方法数,为

leetcode 刷题之路 92 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级(n>=2)台阶,显然只能从第n-1级跳一级到达或