53. Maximum Subarray
来源:互联网 发布:js substr 编辑:程序博客网 时间:2024/06/11 10:08
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [-2,1,-3,4,-1,2,1,-5,4]
,
the contiguous subarray [4,-1,2,1]
has the largest sum = 6
.
使用subarray的方式进行处理
java
class Solution { public int maxSubArray(int[] nums) { if (nums == null || nums.length == 0) { return -1; } int sum = 0; int max = Integer.MIN_VALUE; int min = 0; for (int i = 0; i < nums.length; i++) { sum += nums[i]; max = Math.max(max, sum - min); min = Math.min(sum, min); } return max; }}
python
class Solution(object): def maxSubArray(self, nums): """ :type nums: List[int] :rtype: int """ if nums == None or len(nums) == 0: return -1 value, maximum, minmum = 0, float('-inf'), 0 for element in nums: value += element maximum = max(maximum, value - minmum) minmum = min(value, minmum) return maximum
阅读全文
0 0
- [LeetCode]53.Maximum Subarray
- LeetCode --- 53. Maximum Subarray
- 53.Maximum Subarray
- [Leetcode] 53. Maximum Subarray
- [leetcode] 53.Maximum Subarray
- 53.Maximum Subarray
- 53. Maximum Subarray
- 【leetcode】53. Maximum Subarray
- [leetcode] 53.Maximum Subarray
- 【leetcode】53. Maximum Subarray
- 53. Maximum Subarray
- LeetCode 53. Maximum Subarray
- 53. Maximum Subarray
- [LeetCode]53. Maximum Subarray
- 53. Maximum Subarray LeetCode
- 53. Maximum Subarray
- [LeetCode]53. Maximum Subarray
- 53. Maximum Subarray
- Idea 热部署
- 图像添加LOGO
- 10月12日周四文章
- 冒泡排序算法
- 数字图像处理中的高通滤波和低通滤波
- 53. Maximum Subarray
- CEdit的文本对齐
- java中的参数传递-值传递、引用传递
- 日记—10.12
- Python之 is None VS == None
- 使用vundle管理vim plugins
- 第三方类库-日志-SL4J简介-1
- Ubuntu下vim使用
- HDU-5955-Guessing the Dice Roll