LeetCode: Maximum Subarray 解题报告
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 解题报告的更多相关文章
- Maximum Subarray解题报告zz
http://fisherlei.blogspot.com/2012/12/leetcode-maximum-subarray.html Find the contiguous subarray wi ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- LeetCode - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
- LEETCODE —— Maximum Subarray [一维DP]
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- [LeetCode]Maximum Subarray题解
Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...
- [LeetCode] Maximum Subarray Sum
Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...
随机推荐
- ios实例开发精品文章推荐(8.5)
IOS基础知识记录 IOS基础知识记录一 http://www.apkbus.com/android-131902-1-1.htmlIOS基础知识记录二 http://ww ...
- POJ 2337 Catenyms (欧拉回路)
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8173 Accepted: 2149 Descript ...
- 【Android】Android动态加载Jar、APK的实现
本文介绍Android中动态加载Jar.APK的实现.而主要用到的就是DexClassLoader这个类.大家都知道Android和普通的Java虚拟机有差别,它只能加载经过处理的dex文件.而加载这 ...
- C# Log4net根据日志等级输出到不同文件
原文地址: Log4Net.Config <?xml version="1.0" encoding="utf-8"?> <configurat ...
- ASP.NET MVC同时支持web与webapi模式
原文地址:https://blog.csdn.net/laymat/article/details/65444701 我们在创建 web mvc项目时是不支持web api的接口方式访问的,所以我们需 ...
- centos中添加php扩展pdo_mysql步骤
本文内容是以 CentOS 为例,红帽系列的 Linux 方法应该都是如此,下面就详细说明步骤,在这里严重鄙视哪些内容??隆⑺档脑悠咴影说挠泄 PDO 编译安装的文章. 1.进入 PHP 的软件包 p ...
- struts系列:返回json格式的响应
一.增加依赖库 // https://mvnrepository.com/artifact/org.apache.struts/struts2-json-plugin compile group: ' ...
- SQL Server 2008 R2占用内存越来越大两种解决方法
SQL Server 2008 R2运行越久,占用内存会越来越大. 第一种:有了上边的分析结果,解决方法就简单了,定期重启下SQL Server 2008 R2数据库服务即可,使用任务计划定期执行下边 ...
- windows 自动移动maven jar包到jmeter 文件夹下面
jmeter 自动移动maven jar包到jmeter 文件夹下面 bat 文件 rem 本文件放在jmeter 脚本maven项目根目录下面,和pom.xml在同一个文件夹下面 rem 预置条件, ...
- html中的a标签
<a> 标签定义超链接,用于从一张页面链接到另一张页面.最重要的属性是 href 属性,它指示链接的目标,<href="#">表示跳转到自己.我们通常通过C ...