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 ...
随机推荐
- Windows Office key 持续更新地址
微软 Windows Office 系列序列号,每日都有更新,上面显示的基本都可用,包括MAK及Retail Key. Windows 10 http://textuploader.com/52 ...
- 从零开始设计SOA框架(三):请求参数的加密方式
第二章中说明请求参数有哪些,主要是公共参数和业务参数,服务端需要对参数进行效验,已验证请求参数的合法性 参数效验前先解释下以下参数: 1.参数键值对:包括公共参数.业务参数 1.公共参数:按 ...
- Hibernate-入门教程
首先了解hibernate的目录结构 . +lib antlr.jar cglib-full.jar asm.jar asm-attrs.jars commons-collections.jar co ...
- SOM自组织映射网络 教程
概述 SOM是芬兰教授Teuvo Kohonen提出的一种神经网络算法,它提供一种将高维数据在低维空间进行表示的方法(通常是一维或二维).缩减向量维度的过程,叫做向量量化(vector quantis ...
- 【poj1745】 Divisibility
http://poj.org/problem?id=1745 (题目链接) 题意 给出n串数,可以在其两两之间添加+或-,判断是否存在某种方案使得出的表达式的答案可以整除k. Solution 水题一 ...
- SqlParameter中的size
SqlParameter中size对于需要指定大小的数据库中的数据类型参数有影响[如nvarchar],如果对于这些类型没有指定size则会默认根据赋的值进行推导应该指定的size,而对于那些大小固定 ...
- IOS基础之 (二) 面向对象思想
编写Objective-C程序时,要使用Foundation框架. 什么是框架? 框架(framework)是由很多类(class)组成的库,可以用来编写程序. 对象(Object) 对象可以保存数据 ...
- POJ1328Radar Installation(区间点覆盖问题)
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 68597 Accepted: 15 ...
- jquery中datagrid中getSelected和getSelections的应用
http://blog.sina.com.cn/s/blog_8e50ede90101fff9.html 刚开始使用jquery的datagrid就知道如果要对特定的一行进行编辑,可以是 $('#on ...
- 利用a标签自动解析URL
parseURL // This function creates a new anchor element and uses location // properties (inherent) to ...