我正在尝试解决此问题:
https://leetcode.com/problems/maximum-swap/description/
我的方法基本上是“检查号码中的第一位是否是最大位数。如果不是,则将其与最大位数互换。如果是,则将列表中的第一位放在一边(称为开头),然后对新号码(第一个数字被砍掉的号码)执行相同的操作。”
为此,我编写了一个称为swapper的递归函数,该函数在其他地方调用。我将数字列表输入其中。如果
,它可以正常工作def swapper(digits, beginning):
print 'running: digits, beginning: ', digits, beginning
if len(digits) > 0: #If there is anything left in the number, continue checking if the first digit is the largest)
if digits.index(max(digits)) == 0:
beginning.append(digits[0])
print 'iterating: digits, beginning = ', digits[1:], beginning
swapper(digits[1:], beginning) #recursively run the function with a smaller digit list
else:
digits[digits.index(max(digits))], digits[0] = digits[0],
max(digits)
return beginning + digits
else: #If there is nothing left in the number, return the beginning list
#which is just all the digits at this point
print 'returning beginning'
print 'beginning: ', beginning
return beginning
#pseudocode here: digits = digits(digits of the number)
results = swapper(digits, [])
print 'results: ', results
如果它从未在交换器中命中注释为“ else”的东西,它就可以工作,但是,如果确实如此,它似乎什么也不返回。使用示例编号9973,标准输出为:
running: digits, beginning: [9, 9, 7, 3] []
iterating: digits, beginning = [9, 7, 3] [9]
running: digits, beginning: [9, 7, 3] [9]
iterating: digits, beginning = [7, 3] [9, 9]
running: digits, beginning: [7, 3] [9, 9]
iterating: digits, beginning = [3] [9, 9, 7]
running: digits, beginning: [3] [9, 9, 7]
iterating: digits, beginning = [] [9, 9, 7, 3]
running: digits, beginning: [] [9, 9, 7, 3]
returning beginning
beginning: [9, 9, 7, 3]
results: None
看起来它正确地将数字分区到开始列表中。然后,它会正确打印“ beginning:”开头。该列表存在,并且像我期望的那样是[9,9,7,3]。然后应该返回它。但是,当我在函数之外打印结果时,它只是None。为什么?
它可以通过另一条return语句正确返回(该语句显示“ return starting + digits”)。
答案 0 :(得分:0)
虽然在构建解决方案并更深入地进行递归时函数可以正常工作,但问题是当您到达基本情况后开始尝试退出时。到目前为止,您的代码不会将递归调用的结果传递回上面的调用。
您需要返回递归调用的结果。在您的情况下,将return
放在递归调用swapper(digits[1:], beginning)
前面即可。
这就是所有的样子:
def swapper(digits, beginning):
print 'running: digits, beginning: ', digits, beginning
if len(digits) > 0: #If there is anything left in the number, continue checking if the first digit is the largest)
if digits.index(max(digits)) == 0:
beginning.append(digits[0])
print 'iterating: digits, beginning = ', digits[1:], beginning
# ADDED: return
return swapper(digits[1:], beginning) #recursively run the function with a smaller digit list
else:
digits[digits.index(max(digits))], digits[0] = digits[0], max(digits)
return beginning + digits
else: #If there is nothing left in the number, return the beginning list
#which is just all the digits at this point
print 'returning beginning'
print 'beginning: ', beginning
return beginning
#pseudocode here: digits = digits(digits of the number)
results = swapper([9, 9, 7, 3],[])
print('results: ', results)
输出:
running: digits, beginning: [9, 9, 7, 3] []
iterating: digits, beginning = [9, 7, 3] [9]
running: digits, beginning: [9, 7, 3] [9]
iterating: digits, beginning = [7, 3] [9, 9]
running: digits, beginning: [7, 3] [9, 9]
iterating: digits, beginning = [3] [9, 9, 7]
running: digits, beginning: [3] [9, 9, 7]
iterating: digits, beginning = [] [9, 9, 7, 3]
running: digits, beginning: [] [9, 9, 7, 3]
returning beginning
beginning: [9, 9, 7, 3]
results: [9, 9, 7, 3]