关于爬楼梯记忆自上而下方法的问题
Question regarding to Climbing Stairs Memoization Top-Down Approach
题目是:“你在爬楼梯,需要n步才能到顶。
每次您可以爬 1 或 2 个台阶。您可以通过多少种不同的方式登顶?
自上而下记忆方法的代码是:
class Solution:
def climbStairs(self, n: int) -> int:
def climb(n, memo): #So, here our function will have two values. n is the number of steps and let's call it memo. Memo will be used to show the return value that reduces the recursive calls
if n < 0: #edge case
return 0 #Return None
if n == 0:
return 1 #There is an error while executing the program and it is not performing what it was intended to do
if memo.get(n): #if the number of steps n are taken which is from 1 to 45, then we can use the get method to find the number of steps
return memo[n] #to find the number of recursive calls we will use the memoization method which is
memo[n] = climb(n-1, memo) + climb(n-2, memo) #memoization will return the number of steps be using the previous two steps
return memo[n]#return the value that reduces the recursive calls
return climb(n, {})
我对台词感到困惑
“
if memo.get(n):
return memo[n]
memo[n] = climb(n-1, memo) + climb(n-2, memo)
return memo[n]
”
为什么我们使用两个 'return memo[n]'?我想,
“
if memo.get(n):
memo[n] = climb(n-1, memo) + climb(n-2, memo)
return memo[n]
”是这里描述记忆化想法的方式。
此外,如果我解释代码的注释有误,请指正我,因为我是编程新手。如果我应该实施任何其他更简单和更好的优化方法来解决这个问题,请告诉我。
我之前发布了一些其他问题,有些人以粗鲁的语气回答,尽管我理解我在这里寻求帮助以自学编程。我明白他们的知识很高级,我还差得远呢。所以,如果我能从回答问题的人那里理解代码并学习编程,我将不胜感激。
第一个 return 语句在 if
条件内,并且它 return 是一个已经计算过的值,以避免计算 2 次或更多次相同的操作。
if memo.get(n): #This if is basically checking if the code has already computed the function in n
return memo[n]
#This line never executes if the code has already returned memo[n] in the if condition used to NOT compute multiple times the same operation
memo[n] = climb(n-1, memo) + climb(n-2, memo)
return memo[n]
但是在第二个return语句中,它给出了存储在memo[n]
上的climb(n-1, memo) + climb(n-2, memo)
的计算,并且在代码执行之前从未完成过。
题目是:“你在爬楼梯,需要n步才能到顶。 每次您可以爬 1 或 2 个台阶。您可以通过多少种不同的方式登顶?
自上而下记忆方法的代码是:
class Solution:
def climbStairs(self, n: int) -> int:
def climb(n, memo): #So, here our function will have two values. n is the number of steps and let's call it memo. Memo will be used to show the return value that reduces the recursive calls
if n < 0: #edge case
return 0 #Return None
if n == 0:
return 1 #There is an error while executing the program and it is not performing what it was intended to do
if memo.get(n): #if the number of steps n are taken which is from 1 to 45, then we can use the get method to find the number of steps
return memo[n] #to find the number of recursive calls we will use the memoization method which is
memo[n] = climb(n-1, memo) + climb(n-2, memo) #memoization will return the number of steps be using the previous two steps
return memo[n]#return the value that reduces the recursive calls
return climb(n, {})
我对台词感到困惑 “
if memo.get(n):
return memo[n]
memo[n] = climb(n-1, memo) + climb(n-2, memo)
return memo[n]
” 为什么我们使用两个 'return memo[n]'?我想, “
if memo.get(n):
memo[n] = climb(n-1, memo) + climb(n-2, memo)
return memo[n]
”是这里描述记忆化想法的方式。
此外,如果我解释代码的注释有误,请指正我,因为我是编程新手。如果我应该实施任何其他更简单和更好的优化方法来解决这个问题,请告诉我。
我之前发布了一些其他问题,有些人以粗鲁的语气回答,尽管我理解我在这里寻求帮助以自学编程。我明白他们的知识很高级,我还差得远呢。所以,如果我能从回答问题的人那里理解代码并学习编程,我将不胜感激。
第一个 return 语句在 if
条件内,并且它 return 是一个已经计算过的值,以避免计算 2 次或更多次相同的操作。
if memo.get(n): #This if is basically checking if the code has already computed the function in n
return memo[n]
#This line never executes if the code has already returned memo[n] in the if condition used to NOT compute multiple times the same operation
memo[n] = climb(n-1, memo) + climb(n-2, memo)
return memo[n]
但是在第二个return语句中,它给出了存储在memo[n]
上的climb(n-1, memo) + climb(n-2, memo)
的计算,并且在代码执行之前从未完成过。