【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】
【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(最大子数组和)】的更多相关文章
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] 53. Maximum Subarray 最大子数组
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- 053 Maximum Subarray 最大子序和
给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大.例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4],连续子序列 [4,-1,2,1] 的和最大,为 ...
- 【LeetCode每天一题】Maximum Subarray(最大子数组)
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [leetcode]53. Maximum Subarray最大子数组和
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- Maximum Subarray(最大子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】
[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
[121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Say you have ...
随机推荐
- 初学者最易懂的git教程在这里!
一.git简介: Linux创建了Linux,但是Linux的发展壮大是由世界各地的热心志愿者参与编写的?那么那么多份的代码是怎么合并的呢?之前是在2002年以前,世界各地的志愿者把源代码文件通过di ...
- JavaScript系列-----对象基于哈希存储(<Key,Value>之Key篇) (1)
1.Hash表的结构 首先,允许我们花一点时间来简单介绍hash表. 1.什么是hash表 hash表是一种二维结构,管理着一对对<Key,Value>这样的键值对,Hash表的结构如下图 ...
- 学习笔记-----php搭建用户管理系统
后台:php,数据库:mysql,前端:html,css,js; 主要页面介绍: 1.php连接数据库后台,读取数据并将其以表格的形式显示,并且有添加,编辑,删除,分页等功能: 2.php用于添加用户 ...
- 字符函数 php
strrchr( '123456789.xls' , '.' ); //程序从后面开始查找 '.' 的位置,并返回从 '.' 开始到字符串结尾的所有字符
- github设置
ssh-key: https://help.github.com/articles/generating-ssh-keys http://segmentfault.com/q/101000000013 ...
- 《java.util.concurrent 包源码阅读》25 Fork/Join框架之Fork与Work-Stealing(重写23,24)
在写前面两篇文章23和24的时候自己有很多细节搞得不是很明白,这篇文章把Fork和Work-Stealing相关的源代码重新梳理一下. 首先来看一些线程池定义的成员变量: 关于scanGuard: v ...
- TFBOY 养成记 一些比较好多文章。
API解释中文版(简书文章,没事看看): http://www.jianshu.com/p/e3a79eac554f Tensorlfow op辨异:tf.add()与tf.nn.bias_add() ...
- EntityFramework For Mysql 动态切换数据源
1.简介 在工作中遇到一个问题.项目有三个数据库(三个数据库表结构一样),用户可以选择使用哪个数据库.其实就是动态切换数据库连接. 2.EntityFramework For Mysql 先来简单的介 ...
- 简易RPC框架-熔断降级机制
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- 15. 使用Apache Curator管理ZooKeeper
Apache ZooKeeper是为了帮助解决复杂问题的软件工具,它可以帮助用户从复杂的实现中解救出来. 然而,ZooKeeper只暴露了原语,这取决于用户如何使用这些原语来解决应用程序中的协调问题. ...