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 ...
随机推荐
- HUD 1288 Hat's Tea(反向的贪心,非常好的一道题)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1288 Hat's Tea Time Limit: 2000/1000 MS (Java/Others) ...
- android中OpenMax的实现【3】OMX中主要成员
原文 http://blog.csdn.net/tx3344/article/details/8117908 通过上文知道了,每个AwesomePlayer 只有一个OMX服务的入口,但是Awesom ...
- 【SQLSERVER学习笔记】分页存储过程+调用
USE [数据库名] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].[存储过程名] @pageI ...
- C#强大的编程功能
下面列出一些C#重要的功能 1.布尔条件 2.自动垃圾回收 3.标准库 4.组件版本 5.属性和事件 6.委托和事件管理 7.易于使用的泛型 8.索引器 9.条件编译 10.简单的多线程 11.LIN ...
- ubuntu server遇到的问题
1.在呢用is把隐藏的文件显示出来: ls -a 就可以啦 2.vim退出: 在命令模式中,连按两次大写字母Z,若当前编辑的文档曾被修改过,则Vi保存该文档后退出,返回到shell:若当前编辑的文档没 ...
- 安装sqlserver2016出现的错误
今天安装sharepoint 2016,projectserver 2016,在安装sqlserver2016的时候突然报错了,提示需要安装oracle java se, Rule “Oracle J ...
- oracle使用DBMS_RANDOM包生成随机数据
(一)DBMS_RANDOM包信息 DBMS_RANDOM包包含3个存储过程,4个函数,1个类型,一共8个模块,如下. SQL> desc dbms_random Element Type -- ...
- 关于ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法
转 关于ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法 2016年03月31日 10:14:59 阅读数:1 ...
- Java中BigDecimal的一个除法异常
java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal res ...
- Vue路由钩子 afterEach beforeEach区别
vue-router作为vue里面最基础的服务,学习一段时间,对遇到的需求进行一些总结 使用vue-cli作为开发前提 vue-router已经配置好了 路由写法 routes: [ { path ...