lintcode-medium-Continuous Subarray Sum
2016-03-16 12:43
375 查看
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)
Give
这道题和maximum subarray基本一样,区别在于需要记录index
Give
[-3, 1, 3, -3, 4], return
[1,4].
这道题和maximum subarray基本一样,区别在于需要记录index
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) { // Write your code here ArrayList<Integer> result = new ArrayList<Integer>(); if(A == null || A.length == 0) return result; int index1 = 0; int index2 = 0; int sum = A[0]; int max = A[0]; int res1 = 0; int res2 = 0; for(int i = 1; i < A.length; i++){ if(sum < 0){ sum = 0; index1 = i; index2 = i; } sum += A[i]; if(sum > max){ max = sum; index2 = i; res1 = index1; res2 = index2; } } result.add(res1); result.add(res2); return result; } }
相关文章推荐
- PreparedStatement比Statement好用的问题
- Flask web开发 简单介绍
- 《开源安全运维平台OSSIM最佳实践》当当自营店 315活动 仅售6折
- python异常处理
- android开发之路08(ListView&Adapter)
- android studio 中R文件丢失报错问题
- Android------打招呼
- 异常的补充
- list view 多层表头
- 你好,C++(12)怎样管理多个类型同样性质同样的数据?3.6 数组
- URL和URI的区别
- 数字图像处理-图像的边缘检测
- leetcode 每日一题 27. Remove Element
- struts2自己定义拦截器
- kali 安装火狐
- Problem C: 判断字符串是否为回文
- ddd
- strust2框架的基本搭建
- 使浏览器不进行缓存
- linux --> Event Loop介绍