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 ...
随机推荐
- ArrayList和LinkedList源码分析
ArrayList 非线程安全 ArrayList内部是以数组存储元素的.类有以下变量: /*来自于超类AbstractList,使用迭代器时可以通过该值判断集合是否被修改*/ protected t ...
- Mac 常用快捷键整理
Mac下常用的快捷键: Command+W 将当前窗口关闭(可以关闭Safari标签栏,很实用) Command+Option+M 将所有窗口最小化 Command+Q 关闭当前应用程序(相当于Doc ...
- jQuery+PHP+Mysql在线拍照和在线浏览照片
本文用示例讲述了如何使用jQuery与PHP及Mysql结合,实现WEB版在线拍照.上传.显示浏览的功能,ajax交互技术贯穿本文始末,所以本文的读者要求具备相当熟悉jQuery及其插件使用和javs ...
- 数组去重复及记录重复个数(以及遍历map的四种方法)
private static void check(String[] array) { // 字符串数组中,含有不重复的字符串有哪些?每一个重复的个数 Map<String,Integer> ...
- 大家好,请问在DELPHI中#13和#10是表示什么含义的?
#13: 表示"回车"#10: 表示"换行" ASCII码 Delphi字符 C程序 含义------- ---------- ----- ------ ...
- 【bzoj3560】DZY Loves Math V 欧拉函数
题目描述 给定n个正整数a1,a2,…,an,求 的值(答案模10^9+7). 输入 第一行一个正整数n. 接下来n行,每行一个正整数,分别为a1,a2,…,an. 输出 仅一行答案. 样例输入 3 ...
- Qt5 UI信号、槽自动连接的控件重名
Qt5 UI信号.槽自动连接的控件重名 来源 http://blog.csdn.net/goldenhawking/article/details/51865909 对Qt5稍有熟悉的童鞋都知道信号. ...
- 【BZOJ1088】扫雷(递推)
[BZOJ1088]扫雷(递推) 题面 BZOJ 题解 忽然发现这就是一道逗逼题. 只需要枚举一下第一个是什么,后面都能够推出来了.. #include<iostream> using n ...
- [pool www] user has not been defined
[02-Dec-2014 00:28:58] ALERT: [pool www] user has not been defined [02-Dec-2014 00:28:58] ERROR: fai ...
- 【原创】【2】rich editor系列教程。了解document.execommand操作,保存丢失的range,实时反馈样式给工具栏
[原创][2]rich editor系列教程.了解document.execommand操作,保存丢失的range,实时反馈样式给工具栏 索引目录:http://www.cnblogs.com/hen ...