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. windows 10 安装node.js

    第一步:下载软件 nodejs的中文官网http://nodejs.cn/download/ 选择 windows  系统 msi 安装版本. 下载完成之后,直接打开下一步安装就可以. 安装完成 打开 ...

  2. sublime text 3安装 package control 插件的方法

    自动安装的方法 - 快捷键ctrl+` 或者View->Show Console,输入如下代码 import  urllib.request,os;pf='Package Control.sub ...

  3. 7、Linux应用程序地址布局

    程序构成 在学习Linux应用程序开发时,经常会遇到如下概念: 代码段.数据段.BSS段(Block Started by Symbol,又名:未始化数据段) .堆(heap)和栈(stack).始化 ...

  4. linux线程篇 (二) 线程的基本操作

      线程 进程 标识符 pthread_t pid_t 获取ID pthread_self() getpid() 创建 pthread_create() fork 销毁 pthread_exit() ...

  5. 给无符号数赋值负数(有符号数)的理解(unsigned\signedf)

    无符号数赋负数(有符号数)就类似于给字符型变量赋数值(char word=0x56),对相同的值不同的类型解析 //s16:signed short; u16:unsigned short s16 t ...

  6. Jedis+Redis+spring缓存

    Redis程序使用它?Jedis 访问redis java api Redis-server & //后台运行防火墙要关闭 ts-parent的pom.xml加上jedis依赖 <dep ...

  7. 下载地图瓦片(包括各种格式的,Openstreetmap,googlemap, bingmap)

    参考第三方开源库Brutile 个人的程序托管在github上

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

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

  9. 北京优步UBER司机B组最新奖励政策、高峰翻倍奖励、行程奖励、金牌司机奖励【每周更新】

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

  10. CC3200使用MQTT的SSL加密证书可用日期修改

    1. 在使用CC3200进行SSL加密的时候,需要证书,但是证书有一个截止日期,如果当前CC3200没有设置这个日期,那么证书通信会失败,需要添加代码 int setDeviceTime() { Sl ...