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)

Have you met this question in a real interview?

 
 
Example

Give [-3, 1, 3, -3, 4], return [1,4].

这道题跟LeetCode上的那道Maximum Subarray很类似,不同之处在于这道题让返回最大子数组的范围坐标,而之前那道题只需要返回最大和即可,所以这道题我们要及时更新start和end变量,分别为子数组的起始和结束位置。我们用curSum来记录当前位置的累计和,如果某一个位置之前的累计和为负数,那么我们直接从当前位置开始重新算即可,因为加上负数还不如不加,此时将start和end都更新为当前位置i;如果之前的累计和大于等于0,那么我们把累计和curSum再加上当前的数字,然后更新end位置为i。此时我们更新最大子数组之和mx,以及res即可,参见代码如下:

class Solution {
public:
/**
* @param A an integer array
* @return A list of integers includes the index of
* the first number and the index of the last number
*/
vector<int> continuousSubarraySum(vector<int>& A) {
vector<int> res(, -);
int curSum = , mx = INT_MIN, start = , end = ;
for (int i = ; i < A.size(); ++i) {
if (curSum < ) {
curSum = A[i];
start = end = i;
} else {
curSum += A[i];
end = i;
}
if (mx < curSum) {
mx = curSum;
res[] = start;
res[] = end;
}
}
return res;
}
};

类似题目:

Maximum Subarray

参考资料:

http://www.jiuzhang.com/solutions/continuous-subarray-sum/

[LintCode] Continuous Subarray Sum 连续子数组之和的更多相关文章

  1. lintcode :continuous subarray sum 连续子数组之和

    题目 连续子数组求和 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.(如果两个相同的答案,请返回其中任意一个) 样例 给定 [-3, ...

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

  3. [LeetCode] Minimum Size Subarray Sum 最短子数组之和

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  4. Minimum Size Subarray Sum 最短子数组之和

    题意 Given an array of n positive integers and a positive integer s, find the minimal length of a suba ...

  5. [LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和

    Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...

  6. [LeetCode] 930. Binary Subarrays With Sum 二元子数组之和

    In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0, ...

  7. lintcode循环数组之连续子数组求和

    v 题目:连续子数组求和 II 给定一个整数循环数组(头尾相接),请找出一个连续的子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.如果多个答案,请返回其中任意一个. ...

  8. [LintCode] Continuous Subarray Sum II

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

  9. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

随机推荐

  1. 智能车学习(二十)——浅谈C车硬连接与软连接

    一.为何要追求软连接?       车子进行软连接之后,可以达到一种效果,就是在高速过程中,车子如果快要发生侧翻的时候,只会跳一个后轮,且只是轻微,而前轮如果进行的内倾,就可以让前轮最大面积接触,增大 ...

  2. user-select

    样式详查    http://www.css88.com/book/css/properties/user-interface/user-select.htm 1, user-select: none ...

  3. ImageSpan

    自定义ImageSpan继承类,可以设置图片大小和位置 import android.content.Context; import android.graphics.Bitmap; import a ...

  4. hibernate快速入门

    第一步:下载Hibernate的开发包: http://sourceforge.net/projects/hibernate/files/hibernate3 第二步:Hibernate框架目录结构: ...

  5. cf 710E dp

    题目链接: http://codeforces.com/problemset/problem/710/E 题意:要输入n个字符'a',有两种操作,一种是输入或删除一个'a',耗时x:另一种是把当前的整 ...

  6. Angular JS学习之表达式

    1.Angular JS使用表达式把数据绑定到HTML: 2.Angular JS表达式写在双大括号中:{{expression}} **Angular JS表达式把数据绑定到HTML,这与ng-bi ...

  7. 配置.net连接数据库的配置文件

    今天调试一个学生信息管理系统时,启动系统之后,登录时老是报错说实例有问题,拿着报错信息去找方法也没遇到能解决问题的,最后怀疑是数据库配置和配置文件不匹配, 发现自己的数据库里并没有SQLEXPRESS ...

  8. iOS10 UI教程视图调试

    iOS10 UI教程视图调试 iOS10 UI教程视图调试,当视图很复杂的时候,层次结构就不会很简单了.Xcode可以通过视图(View)调试帮助开发者解决层次结构复杂的问题.视图调试是在Xcode ...

  9. spring mvc 注解访问控制器以及接收form数据的方式,包括直接接收日期类型及对象的方法

    Spring 中配置扫描器 <!-- springmvc的扫描器--> <context:component-scan base-package="com.beifeng. ...

  10. git 学习笔记1--config & help

    1. config 配置用户名和邮箱: git config --global user.name 'pzdn2009' git config --global user.email 10502441 ...