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. 今目标登录时报网络错误E110

    今目标登录的时候报错了,错误代码:E110不论怎么修改都修复不了,百度相关资料也没有,只能联系客服. 经过好久终于联系上了客服,客服给出的解决方案是修改:Enternet选项: 第一步:打开,控制面板 ...

  2. Scrum冲刺博客汇总

    第一篇 Scrum冲刺博客 http://www.cnblogs.com/LZTZ/p/8886296.html 第二篇 Scrum冲刺博客 http://www.cnblogs.com/LZTZ/p ...

  3. 第七周PSP(10.27-11.03)

    psp   进度条 项目 细则 总计 代码行数   0 随笔字数   0 知识点   无 累计曲线 饼图

  4. [学习]WireShark 的过滤功能

    1. 打开 wireShark 过滤显示 协议 比如显示arp协议 过滤栏输入arp即可 支持的协议类型 TCP UDP HTTP FTP ICMP SMTP等等 2. 过滤ip地址 ip.addr ...

  5. py下载网络图片

    很简单,做下记录 import urllib import os url = "图片路径" dir = "d:\\pyimgtest\\G0000001\\" ...

  6. 使用JMeter录制Web应用测试脚本

    环境 操作系统:Windows 7 工具:JMeter.Badboy 1. 使用代理录制Web性能测试脚本 使用代理录制脚本来创建测试计划无疑是一个简便的方法,代理所要完成的工作就是录制发往服务器的请 ...

  7. 对ViewModel自定义约束

    有时候我们常要对一些属性进行自定义的约束,可以这么做 using ListSys.Entity; using System; using System.Collections; using Syste ...

  8. ZOJ3513_Human or Pig

    这个题太坑爹了,题意也好纠结. 是这样的,给你一个n*m的矩形,中间有n*m个1*1的格子,有不同的跳跃方法.如果当前为human(人类)那么他可以有意识的选择自己下一步跳往何方:如果当前为pig(猪 ...

  9. An In-Depth Look at the HBase Architecture--转载

    原文地址:https://www.mapr.com/blog/in-depth-look-hbase-architecture In this blog post, I’ll give you an ...

  10. list+map

    通常读取数据库表中的一条记录后,可以存储于Hashmap变量中:若要读取多条记录,则依次读取每个记录时,先用Hashmap变量存取,然后将Hashmap加到ArrayList变量中. 注意: 每次读取 ...