1. 程式人生 > >leetcode 70 Climbing Stairs (爬樓梯) python3 多種思路(Top down / Bottom up)

leetcode 70 Climbing Stairs (爬樓梯) python3 多種思路(Top down / Bottom up)

class Solution:
    # def __init__(self):
    #         self.dic = {1:1, 2:2}

    def climbStairs(self, n):
        """
        :type n: int
        :rtype: int
        """
        # 這道題本質上是一個斐波那契數列,因為後一個數總是等於前兩個數字之和!

        # 思路一:  Top down - TLE  因為前面已經算過的數字不能直接呼叫,總是需要重新計算.
        # if n == 1:
# return 1 # if n == 2: # return 2 # if n > 2: # return self.climbStairs(n-1) + self.climbStairs(n-2) # Top down + memorization (list) 對於思路一的改進 # def climbStairs4(self, n): # if n == 1: # return 1 # dic = [-1 for i in xrange(n)]
# dic[0], dic[1] = 1, 2 # return self.helper(n-1, dic) # def helper(self, n, dic): # if dic[n] < 0: # dic[n] = self.helper(n-1, dic)+self.helper(n-2, dic) # return dic[n] # Top down + memorization (dictionary) 對於思路一的改進,用一個字典來儲存前面計算過的部分,減少運算時間
# if n not in self.dic: # self.dic[n] = self.climbStairs(n-1) + self.climbStairs(n-2) # return self.dic[n] # Bottom up, O(n) space 思路二:自底向上. 儲存前面計算過的數字,用空間來換取時間 # if n == 1: # return 1 # res = [0 for i in range(n)] # res[0], res[1] = 1, 2 # for i in range(2, n): # res[i] = res[i-1] + res[i-2] # return res[-1] # Bottom up, O(n) space 思路二:自底向上. 進一步壓縮佔用的儲存空間 if n == 1: return 1 a, b = 1, 2 for i in range(2, n): a,b = b,a+b return b

相關推薦

leetcode 70 Climbing Stairs (樓梯) python3 多種思路(Top down / Bottom up)

class Solution: # def __init__(self): # self.dic = {1:1, 2:2} def climbStairs(se

[LeetCode] 70. Climbing Stairs 樓梯

code pytho recursion tin ram pub time stair lee You are climbing a stair case. It takes n steps to reach to the top. Each time you can ei

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 c

LeetCode 70. Climbing Stairs 樓梯

解法一: class Solution { public:     int climbStairs(int n) {         int cur=1,prev=0;         for(int i=1;i<=n;++i)         {          

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

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

leetCode 70.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

[LeetCode] Min Cost Climbing Stairs 樓梯的最小損失

int rom fin [1] case pin cos heap tput On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once

[leetcode 70]Climbing Stairs

col 超時 gin pub bing n-2 consola href rgb You are climbing a stair case. It takes n steps to reach to the top. Each time you can eithe

LeetCode 70:Climbing Stairs

ria san 選擇 bst post take text 樓梯 csdn You are climbing a stair case. It takes n steps to reach to the top. Each time you can eit

[leetcode] 70. Climbing Stairs 解題報告

steps style n-2 遞歸 climb n-1 turn reac pan You are climbing a stair case. It takes n steps to reach to the top. Each time you can either

[Leetcode 70]: Climbing Stairs

div clas lis style tro tair ive script air Description: You are climbing a stair case. It takes n steps to reach to the top. Each time y

[leetcode]70.Climbing Stairs

ret reac The solution 計算 not 動態規劃 man return 題目 You are climbing a stair case. It takes n steps to reach to the top. Each time you can ei

LeetCode 70. Climbing Stairs

nat step tair stair there mission not str sta 分析 難度 易 來源 https://leetcode.com/problems/climbing-stairs/ 題目 You are climbing a stair case.

#LeetCode# 70. Climbing Stairs

https://leetcode.com/problems/climbing-stairs/   You are climbing a stair case. It takes n steps to reach to the top. Each time you can e

[leetcode]70. Climbing Stairs

備忘錄方法(自頂向下的動態規劃) public class Solution { public int climbStairs(int n) { int memo[] = new int[n + 1]; return climb_Stairs(0,

[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

動態迴歸Leetcode 70 Climbing Stairs

Leetcode 70 Climbing Stairs Problem Description: 爬樓梯:一次可以爬1-2層樓梯,求出爬到最高一層的樓梯n時需要的步數。 具體的題目資訊: https://leetcode.com/problems/climbing-

Leetcode 70. Climbing Stairs-每次能走1步或2步臺階,輸入n,求總的方法數

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 w

LeetCode-70. Climbing Stairs (Java)

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