Skip to content

Instantly share code, notes, and snippets.

@zhuifengshen
Last active February 20, 2019 07:40
Show Gist options
  • Save zhuifengshen/ab235324ccc053ba16f952d3b7f4b076 to your computer and use it in GitHub Desktop.
Save zhuifengshen/ab235324ccc053ba16f952d3b7f4b076 to your computer and use it in GitHub Desktop.
列表递归获取最大值
# 实现一
def maxer(lst):
if len(lst) == 0:
return None
if len(lst) == 1:
return lst[0]
else:
sub_max = maxer(lst[1:])
return lst[0] if lst[0] > sub_max else sub_max
# 实现二
def maxer(lst):
if len(lst) == 0:
return None
if len(lst) == 1:
return lst[0]
else:
key = lst.pop()
if lst[0] < key:
lst[0] = key
return maxer(lst)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment