-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path最长子序和.py
46 lines (43 loc) · 841 Bytes
/
最长子序和.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 23 15:57:18 2018
@author: Administrator
"""
nums = [-2,-3,-1,-5]
maxval = 0
temp = 0
i = 0
while 1:
if i == len(nums):
break
if temp < 0:
temp = 0
i = i - 1
else:
temp = temp + nums[i]
maxval = max(temp,maxval)
i = i + 1
if maxval == 0:
maxval= max(nums)
"""
class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if nums==[]:
return None
maxval = 0
temp = 0
for i in nums:
if temp > 0:
temp = temp + i
else:
temp = i
maxval = max(maxval,temp)
if maxval > 0:
return maxval
else:
return max(nums)
"""