lintcode-138-子数组之和
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-子数组之和的更多相关文章
- lintcode:子数组之和为0
题目: 子数组之和 给定一个整数数组,找到和为零的子数组.你的代码应该返回满足要求的子数组的起始位置和结束位置 样例 给出[-3, 1, 2, -3, 4],返回[0, 2] 或者 [1, 3]. 解 ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- Minimum Size Subarray Sum 最短子数组之和
题意 Given an array of n positive integers and a positive integer s, find the minimal length of a suba ...
- [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 ...
- [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, ...
- 求数组的子数组之和的最大值III(循环数组)
新的要求:一维数组改成循环数组,只是涉及简单算法,只是拿了小数做测试 想法:从文件读取数组,然后新建数组,将文件读取的数组在新数组中做一下连接,成为二倍长度的数组,然后再遍历,将每次遍历的子数组的和存 ...
- 求数组的子数组之和的最大值II
这次在求数组的子数组之和的最大值的条件下又增加了新的约束: 1.要求数组从文件读取. 2.如果输入的数组很大, 并且有很多大的数字, 就会产生比较大的结果 (考虑一下数的溢出), 请保 ...
- [LintCode] Continuous Subarray Sum 连续子数组之和
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- lintcode :continuous subarray sum 连续子数组之和
题目 连续子数组求和 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.(如果两个相同的答案,请返回其中任意一个) 样例 给定 [-3, ...
- C#中求数组的子数组之和的最大值
<编程之美>183页,问题2.14——求子数组的字数组之和的最大值.(整数数组) 我开始以为可以从数组中随意抽调元素组成子数组,于是就有了一种想法,把最大的元素抽出来,判断是大于0还是小于 ...
随机推荐
- JS 创建对象总结
狭义:new 构造函数. (注:在JS中创建对象只有一种方式,就是new 构造函数.其中字面量的方式是一种语法糖,本质仍然是new 构造函数) 广义:工厂模式(解决复杂度) 构造函数模式(解决复杂度, ...
- 观看杨老师(杨旭)Asp.Net Core MVC入门教程记录
观看杨老师(杨旭)Asp.Net Core MVC入门教程记录 ASP.NET Core MVC入门 Asp.Net Core启动和配置 Program类,Main方法 Startup类 依赖注入,I ...
- 怎样才能使用ChipScope 加入被优化掉的信号
在调试过程中常常遇到的一个问题就是,xilinx工具在逻辑综合的过程中,将自己RTL代码中的很多变量都优化掉了,使得调试的抓信号的过程很纠结.以下是解决方法: 1.右键synthesis,在综合选项里 ...
- 汇编指令lodsb和stosb、lodsd和stosd
lodsb指令,将esi指向的地址处的数据取出来赋给AL寄存器,esi=esi+1: lodsw指令则取得是一个字. lodsd指令,取得是双字节,即mov eax,[esi],esi=esi+4: ...
- Java基础—IO小结(二)缓冲流与其它流的使用
一.缓冲流的使用 每个字节流都有对应的缓冲流: BufferedInputStream / BufferedOutputStream 构造器: 方法摘要与对应节点流类似 使用缓冲流实现文件复制:实际中 ...
- 北京Uber优步司机奖励政策(1月1日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 成都Uber优步司机奖励政策(1月28日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 【Keras案例学习】 CNN做手写字符分类(mnist_cnn )
from __future__ import print_function import numpy as np np.random.seed(1337) from keras.datasets im ...
- VINS(一)简介与代码结构
VINS-Mono和VINS-Mobile是香港科技大学沈劭劼团队开源的单目视觉惯导SLAM方案.是基于优化和滑动窗口的VIO,使用IMU预积分构建紧耦合框架.并且具备自动初始化,在线外参标定,重定位 ...
- L011系统文件属性知识进阶详解小节
L011系统文件属性知识进阶详解小节 这节课的内容相对来说较少,一上午加中午就听完了,现在总结一下,最后会有一个相关的面试题. 首先先附上一张图: 今天学习主要跟①和②有关,①为Inode 号 ②为文 ...