我只是想做一个简单的hackerrrank question。我想出的代码打印出了答案,但没有返回。我不知道为什么
def myjump(c,j):
if len(c) <= 1:
print("answer: ",j)
return j
elif len(c) > 2:
if c[2] == 0:
c = c[2:]
else:
c = c[1:]
else:
c = c[1:]
j += 1
myjump(c,j)
return "Test"
def jumpingOnClouds(c):
if len(c) <= 1:
return 0
res = myjump(c,0)
print("return: ",res)
return res
result = jumpingOnClouds([0,0,1,0,0,1,0])
输出:
('answer: ', 4)
('return: ', 'Test')
我不明白为什么代码似乎落入了“if len(c)<;=1:”块中并打印数字,但没有命中返回值
你有一个递归函数;递归调用没有
return myjump(c, j)
,因此您正在破坏链(如果这应该是递归的,那么函数调用本身可能是一个错误?)相关问题 更多 >
编程相关推荐