【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. JAVAscript学习笔记 js计时器与倒计时 第六节 (原创) 参考js使用表

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. IE下 GIF不动失效的奇葩问题

    IE下(IE6~IE9都有该问题),对页面进行了某些操作之后,页面上的GIF动画就停留在某一帧不动了~~~ !! 我大IE 就是这么奇葩. 搜索了一下,搞了好久总算搞定. 下面说下目前了解的所有的可能 ...

  3. C#.NET 用程序画图,曲线图

    using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security ...

  4. 引用reference作用域scope闭包closure上下文context用法

    引用(reference).作用域(scope).闭包(closure)以及上下文(context)是JavaScript重中之重的基础,也是学习好JavaScript的基础.在这里我以浅显的理解给大 ...

  5. 使用Jedis操作redis 缓存

    案例:http://blog.csdn.net/linlzk/article/details/41801391 Redis是一个开源的Key-Value数据缓存,和Memcached类似. Redis ...

  6. C# 跨平台的支付类库ICanPay

    随着微软的开源,越来越多的项目支持跨平台,但是各种支付平台提供的类库,又老又不支持跨平台,吐槽下,尤其是微信,还有好多坑,于是ICanPay诞生了,今天就来讲ICanPay是什么,怎么使用? ICan ...

  7. css3实现聊天气泡

    1: <div class="comment"></div><style type="text/css"> .comment ...

  8. HTML页面加载异常,按F12调试后居然又好了的解决办法!

    原因: 你的代码中获取数据那一段应该是有console控制台调用的代码,一般应该是console.log之类的,就是因为这句话在没开F12的时候,console是个undefined的东西就卡在那啦. ...

  9. makefile中伪目标的理解

    1. 我们知道Makefile中的语法是这样: target ... : prerequisites ... command - - 2. 假如编译两个文件可以这么写: a.o:a.c  gcc -c ...

  10. 2723:不吉利日期-poj

    2723:不吉利日期 总时间限制:  1000ms 内存限制:  65536kB 描述 在国外,每月的13号和每周的星期5都是不吉利的.特别是当13号那天恰好是星期5时,更不吉利.已知某年的一月一日是 ...