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的更多相关文章

  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. [转]ASP.NET母版页中对控件ID的处理

    一.问题提出 由于总体排版和设计的需要,我们往往创建母版页来实现整个网站的统一性,最近我由于统一性的需要,把原来整个项目单独的页面全部套用了母版页.但是出现了一个错误……在我的Blog中记录一下,方便 ...

  2. Git--将本地代码提交到服务器分支上

    直接使用git push origin [branch-name],往往会出错,有可能本地版本比分支的版本更低 这个时候需要先将解决冲突,再将本地代码推送到服务器分支上 1. 在自己分支cs上提交代码 ...

  3. UVALive4682 XOR Sum

    UVALive4682 XOR Sum 题意 给定一个数组, 求连续子序列中异或值最大的值. 题解 假设答案区间为 [L, R], 则答案为 XOR[L, R], 可以将区间分解为 XOR[L,R] ...

  4. EF Core中如何通过实体集合属性删除从表的数据

    假设在数据库中有两个表:Person表和Book表,Person和Book是一对多关系 Person表数据: Book表数据: 可以看到数据库Book表中所有的数据都属于Person表中"F ...

  5. php分页方法

    $page_on=15;//定义每页显示数 $pageNum=$_GET['pageNum']; //当前页数 $result = mysql_query("SELECT * FROM ne ...

  6. 【2017001】IList转DataTable、DataTable转IList

    IList转DataTable.DataTable转IList using System; using System.Collections.Generic; using System.Compone ...

  7. 实现一个shell程序

    实现一个自己的shell程序,这个程序有这些功能:解释执行命令,支持输入输出重定向,支持管道,后台运行 程序.当运行该程序后,它支持以下的命令格式: 1.单个命令,如:ls.2.带l到多个参数的命令, ...

  8. Xcode9.2 添加iOS11.2以下旧版本模拟器

    问题起源 由于手边项目需要适配到iOS7, 但是手边的测试机都被更新到最新版本,所以有些潜在的bug,更不发现不了.最近就是有个用户提出一个bug,而且是致命的,app直接闪退.app闪退,最常见的无 ...

  9. python中的super怎么用?

    面向对象有这个强大特点和作用, 著名的三大特点:封装, 继承, 多态 这篇博客写的是super()的简单理解和使用 今天在读restframework的源码的时候, 发现源码中使用了super, 依以 ...

  10. 大专生自学web前端到找到工作的前前后后

    先做个自我介绍,我13年考上一所很烂专科民办的学校,学的是生物专业,具体的学校名称我就不说出来献丑了.13年我就辍学了,我在那样的学校,一年学费要1万多,但是根本没有人学习,我实在看不到希望,我就退学 ...