41. Maximum Subarray
Description
Given an array of integers, find a contiguous subarray which has the largest sum.
The subarray should contain at least one number.
Example
Given the array [−2,2,−3,4,−1,2,1,−5,3], the contiguous subarray [4,−1,2,1] has the largest sum = 6.
Challenge
Can you do it in time complexity O(n)?
这是一道求最大子串和的问题,想解决这个问题,我们需要考虑如下几种情况:
情况一:加上一个正数——那自然最好了,求知不得嘛。且慢,假如前几个数的和为负数,突然来了个正数,是把正数加入旧的队列中,还是这个正数自立门户呢
啥意思?例如,有如此序列 -1,3,5。。。。等等,你是直接从3开始呢,还是把-1这个拖油瓶带着?很明显,为了保证整个子串的值最大,应该从3开始,该思路
对应于下面代码中的注释一。
情况二:加上的数是一个负数——虽然说加上一个负数可能会变小,但有时候我们还是得加上的。例如:3, 4, -5, 6, 7.......虽然加上-5会变小,但是它后面有更大的数,
能让整个序列得和变大。但有时候,加上了一个负数真的会使整个值变小,例如:3,-2, 1 我就三个数,加上-2后,哪怕再加上个一,相对于原来的3来说,还是变小了。
这个时候,就要求我们定义两个变量了,一个来保存最终的结果(res),另一个来“大胆地尝试”(curSum),如果一不小心curSum>res了,则更新res的值。代码如下:
public class Solution {
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
public int maxSubArray(int[] nums) {
// write your code here
int curSum=0;
int res=Integer.MIN_VALUE;
for(int num:nums){
curSum=Math.max(curSum+num,num);//注释一
res=Math.max(curSum,res);
}
return res;
}
}
41. Maximum Subarray的更多相关文章
- [LintCode笔记了解一下]41.Maximum Subarray
Given an array of integers, find a contiguous subarray which has the largest sum. 首先 當題目涉及到求最大最小值時,最 ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- (转)Maximum subarray problem--Kadane’s Algorithm
转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- 算法:寻找maximum subarray
<算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...
- LEETCODE —— Maximum Subarray [一维DP]
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【leetcode】Maximum Subarray
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- maximum subarray problem
In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...
随机推荐
- 跟我一起写 Makefile (Linux )
1.昨天 在 Linux 下用 touch 指令 新建了一个 hello.c 并且使用 vim 编辑器 写了代码 ,使用 gcc 指令编译 最后运行 成功了 .具体方式如下: 在Linux 根目 ...
- 创建Cookie抛出异常:java.lang.IllegalArgumentException: Control character in cookie value
调用Cookie对象的构造函数可以创建Cookie.Cookie对象的构造函数有两个字符串参数:Cookie名字和Cookie值. 名字和值都不能包含空白字符以及下列字符:[ ] ( ) < ...
- 基于java的简易计算器实现
方法: 1.将string类型的表达式输入转换成后缀表达式 2.计算后缀表达式 步骤一:将string类型的表达式输入转换成后缀表达式 输入字符串表达式,并将表达式转换成char型数组 String ...
- 若是将Map作为Key,存入Redis,该如何操作?
1.先封装HashMap Map<String,Object> map=new HashMap<String,Object>(); map.put("name&quo ...
- windows安装多个版本的jdk,解决java-version和javac-version版本不一致的问题
系统先装了jdk1.8 ,环境变量里配置的是jdk1.8,java -version 与javac -version 版本一致. 然后安装了jdk1.6 ,环境变量java_home 改成了1.6,但 ...
- Subclass UICollectionViewFlowLayout,自定义流布局
需求:为实现第一行显示一个,第二行以后显示两个 方案1:用系统自带的流布局,实现的效果是,若第二行只有一个,则系统默认会居中显示,不是左对齐(如下图),不符合项目要求. 方案2:自定义系统的UICol ...
- Ubuntu16 安装Anaconda3+tensorflow cpu版
打开火狐浏览器,下载anaconda安装包,网址:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/?C=M&O=D 下载完成,到Do ...
- CF1066CBooks Queries(数组的特殊处理)
题意描述 您需要维护一个数据结构,支持以下三种操作: L id:在现在序列的左边插一个编号为id的物品 R id:在现在序列的右边插一个编号为id的物品 ? id:查询该点左面有几个元素,右面有几个元 ...
- 解决IDEA右键 new 没有新建class/Interface等等选项
1.File->Project Structure 2.选择Modules-->右边Sources中选择所需目录 然后点击 Sources-->Apply-->OK 3.再在左 ...
- Ajax实现下载进度条
可以通过设置一个XMLHttpRequest对象的 responseType 属性来改变一个从服务器上返回的响应的数据类型.可用的属性值为空字符串 (默认), "arraybuffer&qu ...