【053-Maximum Subarray(最大子数组和)】


【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】

原题

  Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

  For example, given the array [−2,1,−3,4,−1,2,1,−5,4],

the contiguous subarray [4,−1,2,1] has the largest sum = 6.

题目大意

  求数组的最大子数组的和。

解题思路

  动态规划问题。已知了前k个元素的最大子序列和为maxSub(已经被记录下来了),以及一个暂时和sum。假设加入了第k+1这个元素。因为是连续子序列这个限制,所以假设k+1这个元素之前的和是小于0的。那么对于增大k+1这个元素从而去组成最大子序列是没有贡献的,所以能够把sum 置0。

代码实现

算法实现类

public class Solution {

    public int maxSubArray(int[] nums) {
// 參数校验
if (nums == null || nums.length < 1) {
throw new IllegalArgumentException();
} int max = Integer.MIN_VALUE;
int curSum = 0; for (int i : nums) {
// 当前和小于0,就将当前值赋给curSum
if (curSum <= 0){
curSum = i;
}
// 否则进行累加
else {
curSum += i;
} // 保存较大的值
if (max < curSum) {
max = curSum;
}
} return max;
}
}

评測结果

  点击图片,鼠标不释放。拖动一段位置,释放后在新的窗体中查看完整图片。

特别说明

欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47120487

【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】的更多相关文章

  1. [LeetCode] Maximum Subarray 最大子数组

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

  2. [LeetCode] 53. Maximum Subarray 最大子数组

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

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

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

  4. 053 Maximum Subarray 最大子序和

    给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大.例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4],连续子序列 [4,-1,2,1] 的和最大,为 ...

  5. 【LeetCode每天一题】Maximum Subarray(最大子数组)

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  6. [leetcode]53. Maximum Subarray最大子数组和

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  7. [LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  8. Maximum Subarray(最大子数组)

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

  9. 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

    [139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...

  10. 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】

    [121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Say you have ...

随机推荐

  1. iPad web APP 开发相关

    1.移除 browser chrome,全屏启动  <meta name="apple-mobile-web-app-capable" content="yes&q ...

  2. wordpress搬家到 linode 步骤简析

    1. 购买并安装系统 购买就不说了哈,英文不好的自己搜教程. 然后是安装系统 linode 系统安装: 购买完成后登录,进入找到购买的vps ,点击 Dashboard (控制面板) 进入后点击 面板 ...

  3. iOS内置图片瘦身思路整理

    一.前言 前段时间注意到我们APP的包大小超过100MB了,所以随口跟老板说了下能否采用字体文件(.ttf)替代PNG图片,老板对应用瘦身很感兴趣因此让我做下技术调研.这篇文章主要是将我们的各个技术方 ...

  4. NFS服务器的安装与配置

    由于实验室的项目需要实现在CephFS之上建立NFS之上,所以记录一下NFS服务器的安装与配置流程. 1.NFS服务的简介: NFS 是 Network File System 的缩写,是Sun公司于 ...

  5. C# 中操作API

    作为初学者来说,在C#中使用API确是一件令人头疼的问题.在使用API之间你必须知道如何在C#中使用结构.类型转换.安全/不安全代码,可控/不可控代码等许多知识. 一切从简单开始,复杂的大家一时不能接 ...

  6. python基础-------模块与包(二)

    sys模块.logging模块.序列化 一.sys模块 sys.argv           命令行参数List,第一个元素是程序本身路径 sys.exit(n)        退出程序,正常退出时e ...

  7. SAX解析文件

    import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import ja ...

  8. idea for Mac 代码提示设置

    1 打开idea. 2 command+, 打开设置 ,移除Cyclic Expand Word 的快捷键   3 设置basic的快捷键为 option+/ 4,自动提示大小写敏感关闭 apply ...

  9. Did you forget about DBModel.InitializeModel the model [AAAdm] ?

    AIO5安装完毕后登陆出现以下报错:Did you forget about DBModel.InitializeModel the model [AAAdm] ? 说明: 执行当前 Web 请求期间 ...

  10. 常用接口简析3---IList和List的解析

    常用接口的解析(链接) 1.IEnumerable深入解析 2.IEnumerable.IEnumerator接口解析 3.IComparable.IComparable接口解析 学习第一步,先上菜: ...