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. ES6学习笔记四:Proxy与Reflect

    一:Proxy 代理. ES6把代理模式做成了一个类,直接传入被代理对象.代理函数,即可创建一个代理对象,然后我们使用代理对象进行方法调用,即可调用被包装过的方法: 1)创建 var proxy = ...

  2. C# 将一个string数组转换为int数组

    int[] channelCIdArr = Array.ConvertAll(channelIdStr.Split(','),s=>int.Parse(s));

  3. java 读取clob

      java 读取clob CreationTime--2018年7月1日09点41分 Author:Marydon 1.说明 jdbc 数据类型为Clob,与java的String类型相对应. 2. ...

  4. django之 文件上传功能(缺陷:无法改存放目录)

    百度云盘:django之 文件上传功能(缺陷:无法改存放目录)

  5. 转载:mysql添加用户、删除用户、授权、修改密码

    mysql添加用户.删除用户.授权.修改密码等 MySql中添加用户,新建数据库,用户授权,删除用户,修改密码1.新建用户. //登录MYSQL @>mysql -u root -p @> ...

  6. DLL中不能调用CoInitialize和CoInitializeEx

    在项目中为了用API访问Wmi Object来实现命令wmic的功能,所以得使用COM库,使用COM库之前得初始化一些东西. m_hr = CoInitializeEx(, COINIT_APARTM ...

  7. android中的目录结构介绍

      Google Android手机的软件为了安全性和稳定性都是默认安装到手机内存里,但是手机内存有限,所以我们会做app2sd操作,来让我们安装的软件放到sd卡上,这个操作是需要rom的支持的.   ...

  8. (原)使用tensorboard显示loss

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/7416551.html 参考网址: http://blog.csdn.net/jerry81333/ar ...

  9. Swift 互斥锁写法

    oc中的互斥锁@synchronized(self) { //需要执行的代码块} swift中的互斥锁objc_sync_enter(self)//需要执行的代码块objc_sync_exit(sel ...

  10. java mail qq邮箱配置 实例

    程序入口:Test_Email_N.java import java.io.IOException; import java.util.Date; import java.util.Propertie ...