[Lintcode] Continuous Subarray Sum
来源:互联网 发布:淘宝网户外服装男 编辑:程序博客网 时间:2024/05/20 20:19
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 [-3, 1, 3, -3, 4]
, return [1,4]
.
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 */ public ArrayList<Integer> continuousSubarraySum(int[] A) { ArrayList<Integer> res = new ArrayList<Integer>(); if(A.length == 0) return res; //if sum < 0, start in new position int start = 0; int max = Integer.MIN_VALUE; int sum = 0; int maxNeg = Integer.MIN_VALUE, minIndex = 0; for(int i = 0; i < A.length; i++) { if(A[i] > maxNeg) {maxNeg = A[i]; minIndex = i;} sum += A[i]; if(sum <= 0) {sum = 0; start = i + 1;} else { if(sum > max) { max = sum; res.clear(); res.add(start); res.add(i); } } } if(max < 0) { res.add(minIndex); res.add(minIndex); return res; } return res; }}
0 0
- lintcode:Continuous Subarray Sum
- [LintCode] Continuous Subarray Sum
- [Lintcode] Continuous Subarray Sum
- lintcode-Continuous Subarray Sum
- [LintCode] Continuous Subarray Sum II
- Continuous Subarray Sum II
- Continuous Subarray Sum
- 【LeetCode523】. Continuous Subarray Sum
- [LeetCode]Continuous Subarray Sum
- 题解:Continuous Subarray Sum
- 523. Continuous Subarray Sum
- 523. Continuous Subarray Sum
- 523. Continuous Subarray Sum
- leetcode523. Continuous Subarray Sum
- 523. Continuous Subarray Sum
- Continuous Subarray Sum
- 523. Continuous Subarray Sum
- 523. Continuous Subarray Sum
- mysql next-key locking
- POJ2299-Ultra-QuickSort
- 学习Hadoop第四课(Hadoop初始化和测试)
- css的基础内容
- PHP笔记--字符串操作
- [Lintcode] Continuous Subarray Sum
- 字典序问题。在数据加密和数据压缩中常需要对特殊的字符串进行编码。给定的字母表A由26个小写字母组成。该字母表产生的升序字符串中字母从左到右出现的次序与字母在字母表中出现的次序相同,且每个字符最多出现1
- PB-PowerBuilder125开发Webservice的DEMO_从配置IIS到发布接口到调用webservice
- innodb next-key lock解析
- JSP指令
- 算法工程师能力评估
- 经常被问到的java面试题
- 隐式Intent启动Activity,action不能使用@string配置
- LeetCode: Two Sum