Maximum Subarray

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.

SOLUTION 1:

采用滑动窗口解决。sum 如果小于0,置为0,再加上当前值。

然后再与max相比,取大的。 1分钟AC

 public class Solution {
public int maxSubArray(int[] A) {
if (A == null || A.length == 0) {
return 0;
} int max = Integer.MIN_VALUE;
int sum = 0; int len = A.length;
for (int i = 0; i < len; i++) {
if (sum < 0) {
sum = 0;
} sum += A[i];
max = Math.max(max, sum);
} return max;
}
}

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/array/MaxSubArray_1220_2014.java

LeetCode: Maximum Subarray 解题报告的更多相关文章

  1. Maximum Subarray解题报告zz

    http://fisherlei.blogspot.com/2012/12/leetcode-maximum-subarray.html Find the contiguous subarray wi ...

  2. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  3. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  4. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  5. LeetCode - Course Schedule 解题报告

    以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

  6. LEETCODE —— Maximum Subarray [一维DP]

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

  7. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  8. [LeetCode]Maximum Subarray题解

    Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...

  9. [LeetCode] Maximum Subarray Sum

    Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...

随机推荐

  1. 微信支付中的jsapi返回提示信息

    jsapi中跳转到微信支付中触发的方法是js中的getBrandWCPayRequest方法. 改方法中的返回结果msg提示信息如下: err_msg:get_brand_wcpay_request: ...

  2. itunes Connect 未能创建 App 图标

    之前用的是chrome浏览器提交了app和app图标都是没问题的,可今天一直提交一直没成功,也是符合apple要求格式和大小的,郁闷.后来想了想换个浏览器试试,用了mac自带的safari浏览器后居然 ...

  3. 还没被玩坏的robobrowser(5)——Beautiful Soup的过滤器

    背景 本节的知识还是属于Beautiful Soup的内容. Beautiful Soup的find和find_all方法非常强大,他们支持下面一些类型的过滤器. 字符串 最简单的过滤器是字符串.在搜 ...

  4. SharePoint 2013 Farm 安装指南——构建一个双层SharePoint Farm

    最近要对公司里的SharePoint进行升级,由于旧的系统SharePoint 2010已经有2年了,上面改动比较多,而且权限也很混乱了,所以下定决心要对其做一次升级,重新部署一台新的SharePoi ...

  5. Linux下多路复用IO接口epoll/select/poll的区别

    select比epoll效率差的原因:select是轮询,epoll是触发式的,所以效率高. Select: 1.Socket数量限制:该模式可操作的Socket数由FD_SETSIZE决定,内核默认 ...

  6. Python 爬虫编码格式问题 gb2312转换utf8

    遇到的问题是:爬取网页得到的结果如下(部分)  里面的中文出现乱码. <!DOCTYPE html> <html lang='zh-CN'> <head> < ...

  7. ubuntu14.4.4安装smb服务实现文件共享

    1.软件安装,ubuntu14需要安装的软件有3个 安装服务前养成习惯 sudo apt-get upgrade 首先切换到超级用户  su - root sudo apt-get install s ...

  8. python 动态属性

    先来看一下一个常见的Python类的定义: class Person(object): name = None age = None def __init__(self,name,age): self ...

  9. mac os x Server 设置 MIME Types

    最近搞了个 os x Server 把自己mac  配个web 服务器.研究下 ios 企业级应用发布. 网上很多地方都是这个: 对于 OS X Server,将以下 MIME 类型添加到 Web 服 ...

  10. 修改 Semantic UI 中对 Google 字体的引用

    在第一次尝试 Semantic UI 后,发现其 css 中第一行,就引用了 fonts.googleapis.com 中的字体. 不知道为什么要这么做,也许在国外,google 的服务已经是一种互联 ...