LintCode 402: Continuous Subarray Sum
LintCode 402: Continuous Subarray Sum
题目描述
给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大。输出答案时,请分别返回第一个数字和最后一个数字的下标。(如果两个相同的答案,请返回其中任意一个)
样例
给定[-3, 1, 3, -3, 4], 返回[1,4].
Thu Feb 23 2017
思路
本题有很多解法,最巧妙的解法就是一遍扫描记忆的方法了,时间复杂度为\(O(n)\)。
用一个变量记录连续累加的和,当和为负数时,变量清零,从下一个数字开始累加记录。
在这里只需要注意一下一些小细节,比如如何记录最大子数组的起始位置,以及处理一下数组中所有的数都是负数的情况。
代码
// 连续子数组求和
vector<int> continuousSubarraySum(vector<int>& A)
{
int max_sum = -1, sum = 0, start;
int is_all_negative = 1, max_num = -9e5, max_num_ind = -1
vector<int> ans(2);
for (int i = 0; i < A.size(); ++i)
{
if (sum <= 0) start = i;
sum += A[i];
if (sum < 0) sum = 0;
else if (sum > max_sum)
{
is_all_negative = 0;
max_sum = sum;
ans[0] = start;
ans[1] = i;
}
if (max_num < A[i])
{
max_num = A[i];
max_num_ind = i;
}
}
if (is_all_negative)
{
ans[0] = ans[1] = max_num_ind;
}
return ans;
}
LintCode 402: Continuous Subarray Sum的更多相关文章
- lintcode :continuous subarray sum 连续子数组之和
题目 连续子数组求和 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.(如果两个相同的答案,请返回其中任意一个) 样例 给定 [-3, ...
- [LintCode] Continuous Subarray Sum II
Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...
- Continuous Subarray Sum II(LintCode)
Continuous Subarray Sum II Given an circular integer array (the next element of the last element i ...
- 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题是存储的当前累加和的 ...
- [LintCode] Continuous Subarray Sum 连续子数组之和
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- Continuous Subarray Sum
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- [LeetCode] Continuous Subarray Sum 连续的子数组之和
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- [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 ...
- Continuous Subarray Sum LT523
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
随机推荐
- 玩下软工项目,第一轮--全局Context的获取,SQLite的建立与增删改查,读取用户通话记录信息
项目的Github地址:https://github.com/ggrcwxh/LastTime 采用基于git的多人协作开发模式 软件采用mvc设计模式,前端这么艺术的事我不太懂,交给斌豪同学去头疼了 ...
- Windows下IntelliJ IDEA中调试Spark Standalone
参考:http://dataknocker.github.io/2014/11/12/idea%E4%B8%8Adebug-spark-standalone/ 转载请注明来自:http://www.c ...
- playbook详解—YAML格式的文本
在playbook中有一些核心的指令 hosts:指明命令运行在哪个node之上 remote_user:在远端的node之上以什么用户的身份运行命令 var:给模板传递变量值 tasks:指明需要执 ...
- 移植spdylay到libcurl
Libcurl是第三方网络库,支持各种网络协议 SPDY是Google提出的用来替代HTTP1.1的网络协议, 目前google.com, facebook.com, twitter.com服务器端都 ...
- vue h render function & render select with options bug
vue h render function & render select with options bug https://github.com/xgqfrms/vue/issues/41 ...
- solr4.2增量索引之同步(修改,删除,新增)--转载
原文地址:http://www.jiancool.com/article/12743229775/;jsessionid=14E9B3F1BB33399799884B5C8F15DDE1 solr增 ...
- [Code Festival 2017 qual A] B: flip
题意 给出一个n行m列初始全白的矩阵,每次可以翻转一行/一列的全部格子的颜色.问任意次操作后能否使得恰好有k个黑色格子. n,m<=1000 分析 显然要么翻转一次要么不翻转. 最终黑色格子数只 ...
- 题解 P1308 【统计单词数】
小金羊发一篇不一样的题解: 这个题解不是讲解法的,是讲算法的... 众所周知,string在中被定义为是类型, 这意味着我们可以将它作为int一样的类型使用. 并且还有神奇的加减法: string s ...
- Day19内容回顾
1,Django请求的生命周期 路由系统-视图函数(获取模板+数据->渲染)->字符串返回给用户 2,路由系统 /index/ 函数或类.as_view() /detail(\d+)/ 函 ...
- unity3D AR涂涂乐制作浅谈
unity3D AR涂涂乐制作浅谈 AR为现在是虚拟现实较为火爆的一个技术,其中有个比较炫酷的就是AR涂涂乐的玩法,这个技术可以把扫描到的图片上的纹理 粘贴到模型上实现为模型上色的功能,但是我们需要怎 ...