138-子数组之和

给定一个整数数组,找到和为零的子数组。你的代码应该返回满足要求的子数组的起始位置和结束位置

注意事项

There is at least one subarray that it's sum equals to zero.

样例

给出 [-3, 1, 2, -3, 4],返回[0, 2] 或者 [1, 3].

标签

子数组 哈希表

方法一(O(n^2))

利用两个for循环,每次取出一个元素依次与后面的元素相加,时间复杂度是O(n^2)

code

class Solution {
public:
/**
* @param nums: A list of integers
* @return: A list of integers includes the index of the first number
* and the index of the last number
*/
vector<int> subarraySum(vector<int> nums){
// write your code here
int size = nums.size(), sum = 0;
if(size <= 0) {
return vector<int>();
} vector<int> result; for(int i=0; i<size; i++) {
sum = nums[i];
if(sum == 0){
result.push_back(i);
result.push_back(i);
return result;
}
for(int j=i+1; j<size; j++) {
sum += nums[j];
if(sum == 0) {
result.push_back(i);
result.push_back(j);
return result;
}
}
} return result;
}
};

方法二(O(n))

利用一个 map 记录从第一个元素开始到当前元素之和 与 当前元素的下标 的对应关系,若有一段子数组和为0,那么势必出现同一和对应2个下标,此时就找到了和为零的连续序列,时间复杂度是O(n)

code

class Solution {
public:
/**
* @param nums: A list of integers
* @return: A list of integers includes the index of the first number
* and the index of the last number
*/
vector<int> subarraySum(vector<int> nums){
// write your code here
int size = nums.size(), sum = 0;
if(size <= 0) {
return vector<int>();
} vector<int> result;
map<int, int> subSum;
subSum[0] = -1; for(int i=0; i<size; i++) {
sum += nums[i];
if(subSum.count(sum)) {
result.push_back(subSum[sum] + 1);
result.push_back(i);
return result;
}
subSum[sum] = i;
} return result;
}
};

lintcode-138-子数组之和的更多相关文章

  1. lintcode:子数组之和为0

    题目: 子数组之和 给定一个整数数组,找到和为零的子数组.你的代码应该返回满足要求的子数组的起始位置和结束位置 样例 给出[-3, 1, 2, -3, 4],返回[0, 2] 或者 [1, 3]. 解 ...

  2. [LeetCode] Minimum Size Subarray Sum 最短子数组之和

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  3. Minimum Size Subarray Sum 最短子数组之和

    题意 Given an array of n positive integers and a positive integer s, find the minimal length of a suba ...

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

  5. [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, ...

  6. 求数组的子数组之和的最大值III(循环数组)

    新的要求:一维数组改成循环数组,只是涉及简单算法,只是拿了小数做测试 想法:从文件读取数组,然后新建数组,将文件读取的数组在新数组中做一下连接,成为二倍长度的数组,然后再遍历,将每次遍历的子数组的和存 ...

  7. 求数组的子数组之和的最大值II

    这次在求数组的子数组之和的最大值的条件下又增加了新的约束:  1.要求数组从文件读取.      2.如果输入的数组很大,  并且有很多大的数字,  就会产生比较大的结果 (考虑一下数的溢出), 请保 ...

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

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

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

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

  10. C#中求数组的子数组之和的最大值

    <编程之美>183页,问题2.14——求子数组的字数组之和的最大值.(整数数组) 我开始以为可以从数组中随意抽调元素组成子数组,于是就有了一种想法,把最大的元素抽出来,判断是大于0还是小于 ...

随机推荐

  1. 利用HaoheDI从数据库抽取数据导入到hbase中

    下载apache-phoenix-4.14.0-HBase-1.4-bin.tar.gz 将其中的 phoenix-4.14.0-HBase-1.4-client.jar phoenix-core-4 ...

  2. telent connection refused

    1.问题场景 Centos7 做flume案例时,telnet hadoop-senior03.itguigu.com 44444 总是Connection redused, Trying 192.1 ...

  3. Apache Flume简介及安装部署

    概述 Flume 是 Cloudera 提供的一个高可用的,高可靠的,分布式的海量日志采集.聚合和传输的软件. Flume 的核心是把数据从数据源(source)收集过来,再将收集到的数据送到指定的目 ...

  4. [STM32F4][关于看门狗的那些事]

    STM32(stm32f4XX系列)看门狗的总结: 1. 具有两个看门狗外设(独立和窗口)均可用于检测并解决由软件错误导致的故障:当计数器达到给定的超时值时,触发一个中断(仅适用于窗口看门狗)或产生一 ...

  5. linux chrom 系统无法读取用户偏好配置无需删除.config配置文件

    鬼使神差的使用了root权限启用了一下浏览器,再次打开就出现了这样的状况. 百度搜索了一下解决方案 几乎都是同一篇 需要删除/.config/google-chrome文件   才能正常启动. 那么如 ...

  6. java语言描述 用递归打印字符串

    public class Test{ static private int n; public static void main(String[] args) { Test.n=76234; if(n ...

  7. 北京Uber优步司机奖励政策(12月16日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  8. 成都Uber优步司机奖励政策(2月17日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  9. 5 多进程copy文件

    1.如何进行开发? 2.版本1:程序大框架 #1.创建一个文件夹 #2.获取old文件夹中所有的文件名字 #3.使用多进程的方式copy原文件夹中的所有文件到新文件夹中 3.版本2:创建一个文件夹 1 ...

  10. 使用keytool工具产生带根CA和二级CA的用户证书

    使用keytool工具产生带根CA和二级CA的用户证书 1 生成根CA 1.1 生成根CA证书   根CA实际是一张自签CA,自签CA的使用者和颁发者都是它自己.使用下面的命令生成根证书,如果没有指定 ...