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. mvc 页面 去掉转义字符

    mvc 页面 去掉转义字符   mvc 后台返回json数据,用ViewBag 传回前台页面,但是传到前台页面的时候,带有转义字符.一直想去掉这个转义字符,苦恼了好久. 解决方案: mvc 页面有个这 ...

  2. 浅谈Django的中间件与Python的装饰器

    浅谈Django的中间件 与Python的装饰器 一.原理 1.装饰器是Python的一种语法应用,利用闭包的原理去更改一个函数的功能,即让一个函数执行之前先到另外一个函数中执行其他需求语句,在执行该 ...

  3. Golang通道的无阻塞读写的方法示例

    无论是无缓冲通道,还是有缓冲通道,都存在阻塞的情况,但其实有些情况,我们并不想读数据或者写数据阻塞在那里,有1个唯一的解决办法,那就是使用select结构. 这篇文章会介绍,哪些情况会存在阻塞,以及如 ...

  4. JZ2440开发板:用按键点亮LED灯(学习笔记)

    本文是对韦东山嵌入式第一期学习的记录之一,如有您需要查找的信息,可以继续往下阅读. 想要用按键点亮LED灯,就需要知道按键和LED灯的相关信息,这样才可以进行之后的操作.阅读JZ2440的原理图,可以 ...

  5. Applied Cloud Deep Semantic Recognition: Advanced Anomaly Detection(应用云深层语义识别:高级异态检测)

    亚马逊链接 引言 (by Mehdi Roopaei & Paul Rad) 异态检测与情境感知 在数据分析领域,异态检测讲的是在一个数据集中,发现到其中不符合预期模式的物体,动作,行为或事件 ...

  6. fedora19之后的版本安装mysql

    正准备学习linux平台的mysql,却发现在fedora21平台下安装mysql总是失败,查了些资料,总结如下: 错误示范: 按照安装软件的经验,习惯性输入以下的命令: $sudo dnf inst ...

  7. Apache Tomcat 8.5 安全配置与高并发优化

    通常我们在生产环境中,Tomcat的默认配置显然不能满足我们的产品需求,所以很多时候都需要对Tomcat的配置进行调优,以下综合我自己的经验来配置 Tomcat 安全与优化情况,如果你有更好的方案,请 ...

  8. 苏州Uber优步司机奖励政策(12月28日到1月3日)

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

  9. Hadoop: Text类和String类的比较

    一般认为Text类和String类是等价的,但二者之间其实存在着不小差别: 以<Hadoop权威指南>中的案例为例,给定字符串  String s = "\u0041\u00DF ...

  10. Jsp刷新分页模板,很全

      1.用来实现上一页下一页,我直接写到查询页面上 <%--page的分页--%> <style type="text/css"> a { color: # ...