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. WebBrowser控件打开https站点

    背景: 与上一篇博文一样,此文亦是由于开发DropboxAPI中遇到问题衍生出来的.由于需要重定向https类型网站,但自己的https证书是自签名的,总是提示'网站的安全证书存在问题'. 鉴此,查了 ...

  2. POJ 1625 Censored!(AC自动机+DP+高精度)

    Censored! Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 6956   Accepted: 1887 Descrip ...

  3. UIPopoverController 的使用

    #import "ViewController.h" #import "RYColorSelectController.h" #import "RYM ...

  4. android如何实现文件按时间先后顺序排列显示

    <span style="font-size:18px;">File[] files =parentFile.listFiles(fileFilter);//通过fil ...

  5. python运算符和表达式

    算术运算符: 比较运算符: 赋值运算符: 位运算符: 逻辑运算符: 身份运算符: 成员运算符: 运算符优先级:

  6. 自定义 ActionBar 标题与菜单中的文字样式

    自定义标题文字样式 标题样式是 ActionBar 样式的一部分,所以要先定义 ActionBar 的样式 <style name="AppTheme" parent=&qu ...

  7. nefu 197 KMP

    Description 在信息检索系统中一个很重要的环节就是关键字符串的查找,从而很多对自己有用的信息.给你一个很长的一段文字, 和一个关键信息字符串,现在要你判断这段文字里面是否有关键字符串. In ...

  8. 常用函数的DTFT变换对和z变换对

    直接从书上抓图的,为以后查表方便 1.DTFT 2.z变换对

  9. express-14 发送邮件

    简介 Node和Express都没有内置的邮件发送功能,所以必须使用第三方模块.推荐Andris Reinman的Nodemailer SMTP.MSA和MTA 发送邮件的通用语言是简单邮件传输协议( ...

  10. configSections

         由于最近一个项目的数据库变动比较频繁, 为了减少数据层的负担, 打算采用.net的MVC框架, 使用LINQ对付数据层.       这个框架的web.config文件里出现了configS ...