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 ...
随机推荐
- ARM 汇编指令集 特点5:ARM 多级指令流水线
1.为增加处理器指令流 的速度,ARM使用多级流水线. 就是举个例子: mov r1,#0 ,这条指令 分几个人做,一个人从存储器取指令,解码指令中用到的寄存器,寄存器运算. 这样三步 :如果一个人做 ...
- JS异步编程 (2) - Promise、Generator、async/await
JS异步编程 (2) - Promise.Generator.async/await 上篇文章我们讲了下JS异步编程的相关知识,比如什么是异步,为什么要使用异步编程以及在浏览器中JS如何实现异步的.最 ...
- 课时53.video标签第二种格式(掌握)
由于视频数据非常非常的重要,所以五大浏览器厂商都不愿意支持别人都视频格式,所以导致了没有一种视频格式是所有浏览器都支持的,这个时候W3C为了解决这个问题,所以推出了第二种video标签的格式 如何查看 ...
- 转:system.Security.Cryptography C# 加密和解密
以下文转自: http://www.360doc.com/content/13/0122/05/19147_261678471.shtml 总结:注册的时候经过MD5加密存进数据库,在登录的时候需要先 ...
- JSP Cookie 处理
Cookie是存储在客户机的文本文件,它们保存了大量轨迹信息.在servlet技术基础上,JSP显然能够提供对HTTP cookie的支持. 通常有三个步骤来识别回头客: 服务器脚本发送一系列cook ...
- 环境配置之 Debug 和 Release - iOS
便于开发.打包中在不同环境(测试.生产)间属性的切换更加方便便捷流畅,故创建设置此方式方法,希望对大家能有所帮助. 首先,创建 Configurations Setting File(.xcconfi ...
- 竞赛题解 - NOIP2018 赛道修建
\(\mathcal {NOIP2018}\) 赛道修建 - 竞赛题解 额--考试的时候大概猜到正解,但是时间不够了,不敢写,就写了骗分QwQ 现在把坑填好了~ 题目 (Copy from 洛谷) 题 ...
- node的安装和配置
一 . 直接安装node 1. http://nodejs.cn/download/ 根据自己的电脑选择适合的安装包 2.安装 , 无脑下一步 , 可以选择安装路径 , 但是一定要记住 . 3.命令行 ...
- 同步请求和异步请求的区别(理解ajax用)
同步请求:发送方发送数据包后,等待接收方发回响应之后,才能发送下一个数据包的通信方式. 异步请求:发送方发送数据包后,不用等待接收方发回响应,就可以发送下一个数据包的通信方式. 同步通信:要求通信双方 ...
- Git命令行和Xcode结合使用(我来告诉你这行代码谁写的)
现在一直使用Git来管理代码,对于有强迫症的我来说,依旧选择了命令行,下面这段话可以更好的解释我为什么喜欢使用终端敲命令. There are a lot of different ways to u ...