Last active
February 20, 2019 07:40
-
-
Save zhuifengshen/ab235324ccc053ba16f952d3b7f4b076 to your computer and use it in GitHub Desktop.
列表递归获取最大值
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 实现一 | |
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