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的更多相关文章

  1. [LintCode] Continuous Subarray Sum II

    Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...

  2. LintCode 402: Continuous Subarray Sum

    LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...

  3. Continuous Subarray Sum II(LintCode)

    Continuous Subarray Sum II   Given an circular integer array (the next element of the last element i ...

  4. 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题是存储的当前累加和的 ...

  5. [LintCode] Continuous Subarray Sum 连续子数组之和

    Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...

  6. [LeetCode] Continuous Subarray Sum 连续的子数组之和

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

  7. [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 ...

  8. Continuous Subarray Sum LT523

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

  9. [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 ...

随机推荐

  1. UVA 10391 stl

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. Html-Css-iframe的使用

    iframe是作为在网页中嵌套网页的标签 <iframe src="homeIndex_init.html" width="100%" height=&q ...

  3. sql-distinct

    DISTINCT 找出表格内的不同资料的值 语法 SELECT DISTINCT "栏位名" FROM "表格名"; 例如:要在以下的表格,Store_Info ...

  4. 决策树笔记:使用ID3算法

    决策树笔记:使用ID3算法 决策树笔记:使用ID3算法 机器学习 先说一个偶然的想法:同样的一堆节点构成的二叉树,平衡树和非平衡树的区别,可以认为是"是否按照重要度逐渐降低"的顺序 ...

  5. TCP/IP详解 笔记八

    UDP协议 UDP是传输层协议,提供无连接不可靠的数据传输,其优点失效率高,确定确定是无序不可靠. 报文格式 UDP头部 TCP和UDP的端口号是独立的 UDP长度是指UDP数据报的总长度 UDP的校 ...

  6. manifest package

    http://www.android-doc.com/guide/topics/manifest/manifest-intro.html It names the Java package for t ...

  7. Android模拟器端口被占用问题的解决办法

    一.问题描述 今天在Eclipse中运行Android项目时遇到"The connection to adb is down, and a severe error has occured& ...

  8. Facebook内部高效工作PPT指南

    Facebook内部高效工作PPT指南 Facebook 内部分享:不论你如何富有,你都赚不到更多的时间,你也回不到过去.没有那么多的假如,只有指针滴答的时光飞逝和你应该好好把握的现在,以下25张PP ...

  9. Python socket编程之一:

    soket 编程步骤 # -*- coding: utf-8 -*- ################################################################# ...

  10. linux:SUID、SGID详解

    linux:SUID.SGID详解 文章转载至:http://tech.ccidnet.com/art/2583/20071030/1258885_1.html 如果你对SUID.SGID仍有迷惑可以 ...