TreeviewCopyright © aleen42 all right reserved, powered by aleen42
53. 最大子序和
https://leetcode-cn.com/problems/maximum-subarray/
Java
class Solution {
public int maxSubArray(int[] nums) {
int result = nums[0], sum = 0;
for(int num : nums) {
if(sum > 0) sum += num;
else sum = num;
result = Math.max(result, sum);
}
return result;
}
}
Python
'''
Author: Goog Tech
Date: 2020-09-15 13:44:20
LastEditTime: 2020-09-15 13:44:46
Description: https://leetcode-cn.com/problems/maximum-subarray/
FilePath: \leetcode-googtech\#53. Maximum Subarray\Solution.py
WebSite: https://algorithm.show/
'''
class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
for i in range(1, len(nums)):
if nums[i - 1] > 0:
nums[i] += nums[i - 1]
return max(nums)