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的值。代码如下:

  1. public class Solution {
  2. /**
  3. * @param nums: A list of integers
  4. * @return: A integer indicate the sum of max subarray
  5. */
  6. public int maxSubArray(int[] nums) {
  7. // write your code here
  8. int curSum=0;
  9. int res=Integer.MIN_VALUE;
  10. for(int num:nums){
  11. curSum=Math.max(curSum+num,num);//注释一
  12. res=Math.max(curSum,res);
  13. }
  14. return res;
  15. }
  16. }

41. Maximum Subarray的更多相关文章

  1. [LintCode笔记了解一下]41.Maximum Subarray

    Given an array of integers, find a contiguous subarray which has the largest sum. 首先 當題目涉及到求最大最小值時,最 ...

  2. 41. leetcode 53. Maximum Subarray

    53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...

  3. (转)Maximum subarray problem--Kadane’s Algorithm

    转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...

  4. [LintCode] Maximum Subarray 最大子数组

    Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...

  5. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  6. 算法:寻找maximum subarray

    <算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...

  7. LEETCODE —— Maximum Subarray [一维DP]

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  8. 【leetcode】Maximum Subarray

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  9. maximum subarray problem

    In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...

随机推荐

  1. Andorid进阶7—— Ant自动编译打包&发布 android项目

    http://www.cnblogs.com/tt_mc/p/3891546.html Eclipse用起来虽然方便,但是编译打包android项目还是比较慢,尤其将应用打包发布到各个渠道时,用Ecl ...

  2. ARM Linux 内核 panic 之cache 一致性 ——cci-400 cache一致互联

    ARM Linux 内核 panic 之cache 一致性 ——cci-400 cache一致互联 CCI-400 集合了互联和一致性功能,有 2 个 ACE slave 接口和 3 个 ACE-Li ...

  3. 用http.get()简单实现网络验证防止客户不给尾款_电脑计算机编程入门教程自学

    首发于:用http.get()简单实现网络验证防止客户不给尾款_电脑计算机编程入门教程自学 http://jianma123.com/viewthread.aardio?threadid=428 给软 ...

  4. 慎使用sql的enum字段类型

    在sql的优化中,会有同学提到一点:使用enum字段类型,代替其他tinyint等类型.以前这也是不少人喜欢优化的,但是现在细想,是非常不合理的. 优点: 1.可以设置区间范围,比如设置性别:1男2女 ...

  5. 浅谈ETL架构中ODS的作用以及如何在HaoheDI中自动创建ODS表

    什么是ODS表? 在ETL架构中,源数据很少会直接抽取加载到数据仓库EDW,二者之间往往会设置一个源数据的临时存储区域,存储数据在清洗转换前的原始形态,通常被大家称做操作型数据存储,简称ODS,在Ki ...

  6. gem install ruby-odbc失败

    解决: brew install unixodbc gem install ruby-odbc -v '0.99998'

  7. ruby 正则表达式Regexp

    ruby正则表达式在线编辑器:rubular 一般规则: /a/匹配字符a.      /\?/匹配特殊字符?.特殊字符包括^, $, ? , ., /, \, [, ], {, }, (, ), + ...

  8. C语言实现 "谁是凶手?"

    日本某地发生了一件谋杀案,警察通过排查确定杀人凶手必为4个嫌疑犯的一个.以下为4个嫌疑犯的供词.A说:不是我.   a=0B说:是C.   c=1 C说:是D.      d=1D说:C在胡说    ...

  9. C# string 转 byte[]

    string 转 byte[] /// <summary> /// string 转 byte /// </summary> /// <param name=" ...

  10. 第五节 Go数据结构之队列

    一.什么是队列 数据结构里的队列就是模仿现实中的排队.如上图中狗狗排队上厕所,新来的狗狗排到队伍最后,最前面的狗狗撒完尿走开,后面的跟上.可以看出队列有两个特点: (1) 新来的都排在队尾: (2) ...