Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of the first number and the index of the last number.

Example

Given [-3, 1, 2, -3, 4], return [0, 2] or [1, 3].

用hashmap来存从nums[0]到nums[i]的和以及当前下标i,遍历数组求前缀和acc[i],如果发现acc[i]的值已经在hashmap中的,那么说明已经找到一段子数组和为0了。

 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
map<int, int> has;
int sum = ;
has[] = -;
vector<int> res;
for (int i = ; i < nums.size(); ++i) {
sum += nums[i];
if (has.find(sum) != has.end()) {
res.push_back(has[sum] + );
res.push_back(i);
return res;
} else {
has[sum] = i;
}
}
return res;
}
};

Given an integer array, find a subarray where the sum of numbers is between two given interval. Your code should return the number of possible answer.

Example

Given [1,2,3,4] and interval = [1,3], return 4. The possible answers are:

[0, 0]
[0, 1]
[1, 1]
[3, 3]

先求出前缀和,然后枚举求两个前缀和的差。如果差在start与end之间,就给res+1。注意前缀和数组前要插入一个0。

 class Solution {
public:
/**
* @param A an integer array
* @param start an integer
* @param end an integer
* @return the number of possible answer
*/
int subarraySumII(vector<int>& A, int start, int end) {
// Write your code here
vector<int> acc(A);
acc.insert(acc.begin(), );
for (int i = ; i < acc.size(); ++i) {
acc[i] += acc[i-];
}
int tmp, res = ;
for (int i = ; i < acc.size() - ; ++i) {
for (int j = i + 1; j < acc.size(); ++j) {
tmp = acc[j] - acc[i];
if (tmp>= start && tmp <= end) ++res;
}
}
return res;
}
};

[LintCode] Subarray Sum & Subarray Sum II的更多相关文章

  1. LintCode 402: Continuous Subarray Sum

    LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...

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

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

  3. Zero Sum Subarray

    Given an integer array, find a subarray where the sum of numbers is zero. Your code should return th ...

  4. Longest subarray of target sum

    2018-07-08 13:24:31 一.525. Contiguous Array 问题描述: 问题求解: 我们都知道对于subarray的问题,暴力求解的时间复杂度为O(n ^ 2),问题规模已 ...

  5. 32. Path Sum && Path Sum II

    Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if ...

  6. Path Sum,Path Sum II

    Path Sum Total Accepted: 81706 Total Submissions: 269391 Difficulty: Easy Given a binary tree and a ...

  7. LeetCode之“树”:Path Sum && Path Sum II

    Path Sum 题目链接 题目要求: Given a binary tree and a sum, determine if the tree has a root-to-leaf path suc ...

  8. LeetCode Two Sum&Two Sum II - Input array is sorted&3Sum&4Sum 一锅煮题解

    文章目录 Two Sum Two Sum II 3Sum 4Sum Two Sum 题意 给定一个数组,和指定一个目标和.从数组中选择两个数满足和为目标和.保证有且只有一个解.每个元素只可以用一次. ...

  9. 关于数论分块里r=sum/(sum/l)的证明!

    今天的模拟赛里T2要使用到数论分块,里面有一个重要的坎就是关于r=sum/(sum/l)的证明,网上关于这道题的题解里都没有关于这个的证明,那么我就来填补一下: 在以下的文章里,我都会使用lo(x)表 ...

随机推荐

  1. Linux内存管理(转)

    一.内核空间 1.1 页 页(page)是内核的内存管理基本单位. ==> linux/mm_types.h struct page { page_flags_t flags; 页标志符 ato ...

  2. 深入理解JVM(三)——垃圾收集策略具体解释

    Java虚拟机的内存模型分为五个部分.各自是:程序计数器.Java虚拟机栈.本地方法栈.堆.方法区. 这五个区域既然是存储空间,那么为了避免Java虚拟机在执行期间内存存满的情况,就必须得有一个垃圾收 ...

  3. 【DB2】建造测试数据

    建表语句 CREATE TABLE     FUND_DAILY_INCOME     (         ID BIGINT NOT NULL GENERATED ALWAYS AS IDENTIT ...

  4. Linux下axel多线程下载

    axel插件是基于yum下的一个多线程下载 01.下载 wget http://www.ha97.com/code/axel-2.4.tar.gz wget https://files.cnblogs ...

  5. List集合去重的一些方法(常规遍历、Set去重、java8 stream去重、重写equals和hashCode方法)

    1. 常规元素去重 碰到List去重的问题,除了遍历去重,我们常常想到利用Set集合不允许重复元素的特点,通过List和Set互转,来去掉重复元素. // 遍历后判断赋给另一个list集合,保持原来顺 ...

  6. ios中封装网络和tableview的综合运用

    1:封装网络请求 类 #import <Foundation/Foundation.h> #import "ASIFormDataRequest.h" #import ...

  7. ajax, jQuery, jQueryeasyUI

    1.ajax与jQueryajax是jquery库里面的一个被封装好的函数,可以拿来直接使用.没有jquery的话,ajax的使用就得用原生的javascript去写,比较麻烦. 2.jQuery E ...

  8. 如何找出阻止windows睡眠的原因或软件

    1.开始菜单 2.搜索程序和文件里输入 CMD 3.cmd.exe上右键点击以管理员权限运行 4.在cmd黑屏窗口里输入 powercfg -requests如下图所示

  9. [Spring学习笔记 5 ] Spring AOP 详解1

    知识点回顾:一.IOC容器---DI依赖注入:setter注入(属性注入)/构造子注入/字段注入(注解 )/接口注入 out Spring IOC容器的使用: A.完全使用XML文件来配置容器所要管理 ...

  10. Oslo 相机 App

    https://itunes.apple.com/cn/app/osho/id1203312279?mt=8.它支持1:1,4:3,16:9多种分辨率拍摄,滤镜可在取景框的实时预览,拍摄过程可与滤镜实 ...