【leetcode-python-14】面试题 16.17. 连续数列
渣渣原始版(70.23%)
以下两者在<0算max不同。均为70.23%。
class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
for i in nums:
if i >= 0:
break
else:
return max(nums)
max1 = 0
save = 0
for num in nums:
if num + max1 > 0:
max1 += num
else:
max1 = 0
if max1 > save:
save = max1
return save
class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
max1 = nums[0]
for i in nums:
if i >= 0:
break
elif max1 < i:
max1 = i
else:
return max1
max1 = 0
save = 0
for num in nums:
if num + max1 > 0:
max1 += num
else:
max1 = 0
if max1 > save:
save = max1
return save
参考大佬版(70.23%)
老老实实的动态规划。
注:参考题解区腐烂的橘子🍊思路。
class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
max1 = float('-inf')
save = float('-inf')
for num in nums:
max1 = max(num, num+max1)
save = max(save, max1)
return save
新手入坑,多多包涵~~
博客介绍了使用Python解决LeetCode面试题16.17,即寻找连续数列的动态规划方法。博主分享了原始版本和参考优化版本的代码,两者效率相同,均为70.23%通过率,并引用了题解区腐烂的橘子�的思路。


被折叠的 条评论
为什么被折叠?



