Skip to content

Commit

Permalink
求最大和
Browse files Browse the repository at this point in the history
  • Loading branch information
qiwsir committed Jun 5, 2014
1 parent f593683 commit b021292
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
25 changes: 25 additions & 0 deletions max_sum.md
@@ -0,0 +1,25 @@
#问题

最大连续子数组,求一个有正,有负数的数组(有正和负数,没有全是负数的情况),连续子数组之最大和。
要求时间复杂度为O(n)

#解决(Python)

#coding:utf-8

def max_array(lst):
this_sum = 0
max_sum = 0
for item in lst:
this_sum += item
if this_sum > max_sum:
max_sum = this_sum
elif this_sum < 0:
this_sum = 0
return max_sum

test_lst = [-2,11,-4,13,-5,-2]
print(max_array(test_lst))

##迪艾姆python培训 黄哥所写 咨询:qq:1465376564
##迪艾姆python远程视频培训班
21 changes: 21 additions & 0 deletions max_sum.py
@@ -0,0 +1,21 @@
#coding:utf-8

def max_array(lst):
"""面试题:最大连续子数组,求一个有正,有负数的数组(有正和负数,没有全是负数的情况),
连续子数组之最大和。
迪艾姆python培训 黄哥所写 咨询:qq:1465376564
迪艾姆python远程视频培训班
要求时间复杂度为O(n)
"""
this_sum = 0
max_sum = 0
for item in lst:
this_sum += item
if this_sum > max_sum:
max_sum = this_sum
elif this_sum < 0:
this_sum = 0
return max_sum

test_lst = [-2,11,-4,13,-5,-2]
print(max_array(test_lst))

0 comments on commit b021292

Please sign in to comment.