【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. css 权重 特殊性

    选择器的特异性由 CSS2 规范定义如下:     !important的规则比其他的权值都大     p{color: red !important} 如果声明来自于“style”属性,而不是带有选 ...

  2. SQL Server 通过SQL脚本启动Broker并设置兼容性

    SQL Server数据库中通过SQL启用Broker 并建立相关队列和服务 兼容代码使其可以在2000库中执行不报错 针对的是2008版本, 如果是其他版本可以改相关版本号和兼容性标记 /***** ...

  3. python学习总结笔记(一)

    1.raw_input("请输入:")提示录入信息,读取录入的字符串返回你录入的字符串2.os.environ 获取所有系统的环境变量,返回一个字典.3.str与repr区别str ...

  4. 1)C语言简介(C自考学习)

    C语言历史由来 世界上第一个高级语言是"ALFOL",而C的前身是ALGOL语言.1970年美国贝尔实验室的肯·汤普逊对BCPL(基本复合程序设计语言)进行了进一步的简化,突出了硬 ...

  5. get和post请求及函数调用模式

    1.get和post请求的应用场景? get: 1.get请求获取(查询)数据 2.请求url长度比较短 3.可以被缓存 4.请求url可以作为浏览器书签 5.可以被保存在浏览器记录中 6.请求参数在 ...

  6. 初学者没有搞明白的GOROOT,GOPATH,GOBIN,project目录

    我们接下来一个一个来看关于Go语言中的三个目录的详细解释先通过go env查看go的环境变量(我这里是mac的环境,所以可能和你的不同) localhost:~ zhaofan$ go env GOA ...

  7. 开源项目 log4android 使用方式详解

    话不多说, 直接上主题. log4android 是一个类似于log4j的开源android 日志记录项目. 项目基于 microlog 改编而来, 新加入了对文件输出的各种定义方式. 项目地址: 点 ...

  8. Android studio导出配置

    在使用 Android Studio 时,往往会进行一些设置,比如 界面风格.字体.字体大小.快捷键.常用模板等.但是这里的设置只能用在一个版本的 Android Studio 上,如果下载了新的 A ...

  9. Session、Cookie 学习笔记

    在开始今天的博文之前首先为自己庆祝一下自己有了三个粉丝,也有了同僚的评论,说实话因为这个开心了好久!哈哈,好了在开始今天的正题之前,首先大家需要了解以下几点: a. HTTP 协议是无状态的协议,WE ...

  10. Java开发步骤

    3.编辑Java源程序 使用纯文本编辑器,比如记事本notpad.exe:EditPlus.UltraEdit等专业的纯文本编辑器. Word不是纯文本编辑器. 需求:写一个Java程序,在控制台打印 ...