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 ...
随机推荐
- 推荐系统 BPR 算法求解过程
数据假设: 每个用户之间的偏好行为相互独立 同一用户对不同物品的偏序相互独立 则优化问题为极大化如下目标: [Reference] 1.论文翻译:BPR:面向隐偏好数据的贝叶斯个性化排序学习模型 2. ...
- lucene之中文分词及其高亮显示
参考:http://www.cnblogs.com/lirenzhujiu/p/5914174.html http://www.cnblogs.com/xing901022/p/3933675.htm ...
- android 安卓APP获取手机设备信息和手机号码的代码示例
下面我从安卓开发的角度,简单写一下如何获取手机设备信息和手机号码 准备条件:一部安卓手机.手机SIM卡确保插入手机里.eclipse ADT和android-sdk开发环境 第一步:新建一个andro ...
- 【脚下生根】之深度探索安卓OpenGL投影矩阵
世界变化真快,前段时间windows开发技术热还在如火如荼,web技术就开始来势汹汹,正当web呈现欣欣向荣之际,安卓小机器人,咬过一口的苹果,winPhone开发平台又如闪电般划破了混沌的web世界 ...
- 【Java】解析JScrollPane类的使用
在这篇博文中,笔者介绍JScrollPane类的使用,JScrollPane类可以为组件添加滚动条.在这里笔者不会详细介绍该类的方法有哪些,因为在API上已经写得一清二楚了.在这篇博文中,笔者重点介绍 ...
- mysql主从复制配置问题
一,基本步骤 1,创建在主从数据上都创建复制账号,权限选上super, replication slave , replication master(选上这个可以方便从库变成主库): 2,配置主库和备 ...
- 打开Word时出现“The setup controller has encountered a problem during install. Please ...”什么意思
解决办法:找到C:\Program Files\Common Files\Microsoft Shared\OFFICE12\Office Setup Controller,将这个文件夹删除或改名,就 ...
- Win7系统计算机中Msvcr100.dll丢失的解决办法
1.使用安全卫士里的人工服务. 在搜索框里输入msvcr100.dll. 点击查找方案. 2.点击msvcr100.dll问题后面的立即修复. 只要等待片刻就好了.
- FreeSWITCH网关参数之caller-id-in-from
1. 这个配置项两个设置值: true和false(默认) <param name="caller-id-in-from" value="true"/&g ...
- Java读取excel的示例
一.引用的jar包,apache的POI // https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml compile group: ' ...