[LintCode] Continuous Subarray Sum 连续子数组之和
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 [-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;
}
};
类似题目:
参考资料:
http://www.jiuzhang.com/solutions/continuous-subarray-sum/
[LintCode] Continuous Subarray Sum 连续子数组之和的更多相关文章
- lintcode :continuous subarray sum 连续子数组之和
题目 连续子数组求和 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.(如果两个相同的答案,请返回其中任意一个) 样例 给定 [-3, ...
- [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 ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- Minimum Size Subarray Sum 最短子数组之和
题意 Given an array of n positive integers and a positive integer s, find the minimal length of a suba ...
- [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 ...
- [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, ...
- lintcode循环数组之连续子数组求和
v 题目:连续子数组求和 II 给定一个整数循环数组(头尾相接),请找出一个连续的子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.如果多个答案,请返回其中任意一个. ...
- [LintCode] Continuous Subarray Sum II
Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
随机推荐
- 解决因为I_JOB_NEXT问题导致job执行不正常,不停生成trace文件问题
今天同事说有个项目生产环境的目录老是满.查看了一下bdump目录,发现确实是平均1分钟生成一个8M左右的trace文件.查询了一下alert日志,发现是个job的报错引起的.具体查看了一下trace文 ...
- 中断处理流程,ok6410
中断处理流程 CPU在工作的过程中,经常需要与外设进行交互,交互的方式包括”轮询方式”,”中断方式”. 1.轮询方式: CPU不断地查询设备的状态.该方式实现比较简单,但CPU利用率很低,不适合多任务 ...
- LeetCode39/40/22/77/17/401/78/51/46/47/79 11道回溯题(Backtracking)
LeetCode 39 class Solution { public: void dfs(int dep, int maxDep, vector<int>& cand, int ...
- JMockit
[TOC] 简介 JMockit是基于JavaSE5中的java.lang.instrument包开发,内部使用ASM库来动态修改java的字节码,使得java这种静态语言可以想动态脚本语言一样动态设 ...
- Atom 和 markdown 基本使用
Atom 和 markdown 基本使用 Atom 常用的快捷键 Cmd + Shift + P 打开命令窗口,可以运行各种菜单功能. Cmd + T 快速的多文件切换 Ctrl + G 文件内跳转到 ...
- 我的 Unity2D 屏幕适配
以下方法纯属我YY,切勿当真!!! 确定一个设计尺寸,比如 devWidth = 960,devHeight = 640, 按照这个尺寸进行设计游戏. 方式一: 不管什么屏幕尺寸,都和设计的尺寸对应. ...
- RMI的概念
RMI(Remote Method Invocation)远程方法调用是一种计算机之间利用远程对象互相调用实现双方通讯的一种通讯机制.使用这种机制,某一台计算机上的对象可以调用另外一台计算机上的对象来 ...
- u盘中放入大于4g单独文件失败解决
u盘中文件格式通常为FAT32,需要转换为NTFS格式 在win7中cmd调出命令行,输入: convert H: /fs:ntfs (H为u盘位置)
- RabbitMQ常用命令
创建一个用户为mytest,密码为mytest rabbitmqctl add_user mytest mytest 删除一个用户 rabbitmqctl delete_user username ...
- (转)解决:本地计算机 上的 OracleOraDb10g_home1TNSListener服务启动后停止
原文地址:http://justsee.iteye.com/blog/1320059 手动启动一个问题:本地计算机 上的 OracleOraDb10g_home1TNSListener服务启动后停止. ...