国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

070. Climbing Stairs

jay_tian / 826人閱讀

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:

Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
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

Solution:

class Solution1:
    """
    遞歸做法
    """
    def climbStairs(self, n):
        """
        :type n: int
        :rtype: int
        """
        if n == 1:
            return 1
        if n == 2:
            return 2
        return self.climbStairs(n-1) + self.climbStairs(n-2)


class Solution2:
    """
    非遞歸做法
    """
    def climbStairs(self, n):
        """
        :type n: int
        :rtype: int
        """
        pre_1_step = 1
        pre_2_step = 0
        count = 1
        for i in range(1, n+1):
            count = pre_1_step + pre_2_step
            pre_2_step = pre_1_step
            pre_1_step = count
        return count

class Solution:
    """
    非遞歸做法,比上一個(gè)方法快
    """
    def climbStairs(self, n):
        """
        :type n: int
        :rtype: int
        """
        F = [0, 1]
        count = 1
        for i in range(n):
            count = F[0] + F[1]
            F[0] = F[1]
            F[1] = count
        return count

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/42048.html

相關(guān)文章

  • [Leetcode] Climbing Stairs 爬樓梯

    摘要:遞歸法復(fù)雜度時(shí)間空間思路這題幾乎就是求解斐波那契數(shù)列。最簡(jiǎn)單的方法就是遞歸。但重復(fù)計(jì)算時(shí)間復(fù)雜度高。代碼遞推法復(fù)雜度時(shí)間空間思路實(shí)際上我們求的時(shí)候只需要和的值,所以可以減少一些空間啊。 Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you c...

    tinyq 評(píng)論0 收藏0
  • [leetcode] Climbing Stairs

    摘要:實(shí)質(zhì)就是把之前出現(xiàn)過的中間結(jié)果記錄,下次再出現(xiàn)相同情況的時(shí)候,通過可以只用的時(shí)間復(fù)雜度得到。表示到達(dá)第層樓梯的不同走法。那么題目中每次可以選擇走一步,或者兩步,。從迭代公式可以知道,有兩個(gè),和。 70. Climbing Stairs You are climbing a staircase. It takes n steps to reach to the top.Each tim...

    int64 評(píng)論0 收藏0
  • 746. Min Cost Climbing Stairs

    746. Min Cost Climbing Stairs On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay the cost, you can either climb one or two steps. You need to find mini...

    skinner 評(píng)論0 收藏0
  • [LintCode] Climbing Stairs II

    Problem A child is running up a staircase with n steps, and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs. Ex...

    chengtao1633 評(píng)論0 收藏0
  • [LintCode] Climbing Stairs

    摘要:無(wú)需動(dòng)規(guī),無(wú)需額外空間,等同于菲波那切數(shù)列。當(dāng)然嚕,也可以動(dòng)規(guī),記住就好。 Problem 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 ...

    jemygraw 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<