53. Maximum Subarray (JAVA)
iven an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
Follow up:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
法I: 动态规划法
class Solution {
public int maxSubArray(int[] nums) {
int maxSum = Integer.MIN_VALUE; //注意有负数的时候,不能初始化为0
int currentSum = Integer.MIN_VALUE;
for(int i = 0; i < nums.length; i++){
if(currentSum < 0) currentSum = nums[i];
else currentSum += nums[i];
if(currentSum > maxSum) maxSum = currentSum;
}
return maxSum;
}
}
法II:分治法
class Solution {
public int maxSubArray(int[] nums) {
return partialMax(nums,0,nums.length-1);
}
public int partialMax(int[] nums, int start, int end){
if(start == end) return nums[start];
int mid = start + ((end-start) >> 1);
int leftMax = partialMax(nums,start, mid);
int rightMax = partialMax(nums,mid+1,end);
int maxSum = Math.max(leftMax,rightMax);
int lMidMax = Integer.MIN_VALUE;
int rMidMax = Integer.MIN_VALUE;
int current = 0;
for(int i = mid; i >= start; i--){
current += nums[i];
if(current > lMidMax) lMidMax = current;
}
current = 0;
for(int i = mid+1; i <= end; i++){
current += nums[i];
if(current > rMidMax) rMidMax = current;
}
if(lMidMax > 0 && rMidMax > 0) maxSum = Math.max(lMidMax + rMidMax,maxSum);
else if(lMidMax > rMidMax) maxSum = Math.max(lMidMax,maxSum);
else maxSum = Math.max(rMidMax,maxSum);
return maxSum;
}
}
53. Maximum Subarray (JAVA)的更多相关文章
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- LN : leetcode 53 Maximum Subarray
lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...
- Leetcode之53. Maximum Subarray Easy
Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
随机推荐
- SWPU2019 伟大的侦探
01editor 选择 EBCDIC编码得到压缩包的密码 这里用到的是福尔摩斯里面的跳舞的小人加密 结果是:iloveholmesandwllm
- wannafly 挑战赛8 E 小G的项链(manecher)
链接:https://www.nowcoder.com/acm/contest/57/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit ...
- php GD库简单使用和封装
GD库创建图像步骤 <?php //1.创建画布 $width = 300; $height= 200; $image=imagecreatetruecolor($width,$height); ...
- hibernate中的@GeneratedValue与@GenericGenerator
1.GeneratedValue与GenericGenerator的区别 @GeneratorValue注解----JPA通用策略生成器 @GenericGenerator注解----自定义主键生成策 ...
- yanse
0E71F9 左上面蓝色 0EA8FF 左下面蓝色 498B54 右边绿色
- STOMP协议详解
STOMP协议详解 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 一.STOMP协议介绍 STOMP即Simple (or Streaming) T ...
- Promethus
https://blog.csdn.net/zl1zl2zl3/article/details/74332437
- 四十一、python面向对象二
A.成员: 1.字段:静态字段(每个对象都有同一字段),普通字典(每个对象都有不同的数据) 2.方法:静态方法(无需使用对象的内容),类方法,普通方法(使用对象中的数据) 3.特性:普通特性(将方法伪 ...
- 源特定组播(SSM:Source Specific Multicast)
源特定组播(SSM:Source Specific Multicast)是一种区别于传统组播的新的业务模型,它使用组播组地址和组播源地址同时来标识一个组播会话,而不是向传统的组播服务那样只使用组播组地 ...
- Mybatis使用时 resultMap与resultType、parameterMap与 parameterType的区别
Map:映射:Type:Java类型 resultMap 与 resultType.parameterMap 与 parameterType的区别在面试的时候被问到的几率非常高,出现的次数到了令人 ...