Continuous Subarray Sum
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your code should return the index of the first number and the index of the last number. (If their are duplicate answer, return anyone)
Example
Give A = [-3, 1, 3, -3, 4]
, return [1,4]
.
分析:
max(i) = {max(i - 1) + A[i] if max(i - 1) >= 0, otherwise, A[i]}
public class Solution {
/**
* @param A an integer array
* @return A list of integers includes the index of the first number and the index of the last number
* cnblogs.com/beiyeqingteng/
*/
public ArrayList<Integer> continuousSubarraySum(int[] A) {
if (A == null || A.length == ) return null; ArrayList<Integer> list = new ArrayList<Integer>();
// initialization
int preMax = A[];
int startIndex = ;
int tempMax = A[];
list.add();
list.add(); for (int i = ; i < A.length; i++) {
if (preMax < ) {
preMax = A[i];
startIndex = i;
} else {
preMax = A[i] + preMax;
}
// update
if (preMax > tempMax) {
list.clear();
list.add(startIndex);
list.add(i);
tempMax = preMax;
}
}
return list;
}
}
转载请注明出处:cnblogs.com/beiyeqingteng/
Continuous Subarray Sum的更多相关文章
- [LintCode] Continuous Subarray Sum II
Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...
- LintCode 402: Continuous Subarray Sum
LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...
- Continuous Subarray Sum II(LintCode)
Continuous Subarray Sum II Given an circular integer array (the next element of the last element i ...
- leetcode 560. Subarray Sum Equals K 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)
整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...
- [LintCode] Continuous Subarray Sum 连续子数组之和
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- [LeetCode] Continuous Subarray Sum 连续的子数组之和
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- [Swift]LeetCode523. 连续的子数组和 | Continuous Subarray Sum
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- Continuous Subarray Sum LT523
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- [leetcode]523. Continuous Subarray Sum连续子数组和(为K的倍数)
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
随机推荐
- FusionCharts的使用方法
来源于:http://www.cnblogs.com/xuhongfei/archive/2013/04/12/3016882.html 今天统计价格变化规律的时候找到的一个很好的文档,很详细 一.简 ...
- MyEclipse 启动报错:'Building workspace' has encountered a problem解决方法
转载于:http://blog.csdn.net/v123411739/article/details/42645159 每次MyEclipse工作空间报错如下:'Building workspace ...
- 判断一个数据是否存在于一个表中,Oracle中写自定义函数
create or replace function isExist(data in DataTypes) --DataTypes 为表中该数据的类型return Numberisv_flag num ...
- 模拟Modbus协议问题
问题: 在嵌入式系统开发中,Modbus协议是工业控制系统中广泛应用的一种协议.本题用来简单模拟Modbus协议,只需根据条件生成符合该协议的数据帧,并解析所获取的数据.假设设备使用的协议发送数据格式 ...
- Bzoj3893 [Usaco2014 Dec]Cow Jog
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 302 Solved: 157 Description The cows are out exerci ...
- CodeReview Learning
目录 . 引言 . 代码检视的指导思想 . 代码检视的内容 . 回归测试 0. 引言 代码检视(Code Review)是指软件开发人员在完成代码设计.编写.调试后展开的个人或群体性的代码阅读过程,代 ...
- 轻量级应用开发之(01)第一个IOS程序
一 IPhone轻量级开发 1. 开发环境 Mac 版本: OS X EICap 10.11.3 (15D21) XCode开发版本: Version 7.2.1 (7C1002) 2.简单分析 UI ...
- P1049送给圣诞夜的礼品(矩阵十大问题之四)
https://vijos.org/p/1049 P1049送给圣诞夜的礼品 Accepted 标签:组合数学送给圣诞夜的礼物[显示标签] 返回代码界面 | 关闭 Pascal Pasca ...
- LocalDB简介和在VS2012及以上版本的使用
http://www.cnblogs.com/fzrain/p/3812364.html?utm_source=tuicool LocalDB简介和在VS2012及以上版本的使用 之前一不小心把自 ...
- VirtualBox安装debian的详细方法步骤
下面是用VirtualBox安装Debian6的方法和步骤 l 新建一个文件夹,用于存放虚拟硬盘,如Debian l 打开VirtualBox,点击新建 l 输入虚拟机名称,Debian_6 l 给虚 ...