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的更多相关文章

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

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

  2. [LintCode] Continuous Subarray Sum II

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

  3. Continuous Subarray Sum II(LintCode)

    Continuous Subarray Sum II   Given an circular integer array (the next element of the last element i ...

  4. 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题是存储的当前累加和的 ...

  5. [LintCode] Continuous Subarray Sum 连续子数组之和

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

  6. Continuous Subarray Sum

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

  7. [LeetCode] Continuous Subarray Sum 连续的子数组之和

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

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

  9. Continuous Subarray Sum LT523

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

随机推荐

  1. 新手使用github过程记录

    初次接触github,记录下我的使用过程.一开始确实有些懵,但好在网上这类的教程有很多,过程也很详细易懂,按照网上的教程走完全没问题,感谢无私分享辛苦整理的各位前辈们. 注册github账号 创建一个 ...

  2. Alpha 冲刺6

    队名:日不落战队 安琪(队长) 今天完成的任务 回收站前端界面. 明天的计划 查看个人信息界面. 还剩下的任务 信息修改前端界面. 设置界面. 遇到的困难 模拟机莫名其妙就崩了,调试了很久,后在队友的 ...

  3. 03_Java基础语法_第3天(Scanner、Random、流程控制语句)_讲义

    今日内容介绍 1.引用类型变量的创建及使用 2.流程控制语句之选择语句 3.流程控制语句之循环语句 4.循环高级 01创建引用类型变量公式 * A: 创建引用类型变量公式 * a: 我们要学的Scan ...

  4. nodejs 中on 和 emit

    首先测试用例: var EventEmitter = require('events').EventEmitter var life = new EventEmitter(); // life.on( ...

  5. PAT 甲级 1005 Spell It Right

    https://pintia.cn/problem-sets/994805342720868352/problems/994805519074574336 Given a non-negative i ...

  6. 自己编写 Oracle 分页函数

    CREATE OR REPLACE PACKAGE PACK_PAGINATION AS PAGESIZE CONSTANT ; TYPE TYRECORD_EMP IS RECORD( EMPNO ...

  7. 【php】提交的特殊字符会被自动转义

    在处理mysql和GET.POST的数据时,常常要对数据的引号进行转义操作. PHP中有三个设置可以实现自动对’(单引号),”(双引号),\\(反斜线)和 NULL 字符转移. PHP称之为魔术引号, ...

  8. Linux服务器启动后只读解决办法

    今天处理一个服务器,远程死活连接不上,只好跑信息中心去看了下服务器. Linux服务器启动之后,提示: give root password for maintenance (or type cont ...

  9. Tomcat安装及配置详解

    Tomcat安装及配置详解   一,Tomcat简介 Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,Tomcat是Apache 软件基金会(Apache Software Found ...

  10. CodeChef KnightMov

    码死了...考试的时候基本上是写一会儿思考一会儿人生....考完了调了调...最后400行+....不应该这么长的....以后重写一下再补题解..... 也许这就是蒟蒻吧.jpg 安利cstdio博客 ...