Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.
Example 1:
Input:nums = [1,1,1], k = 2
Output: 2
Note:
The length of the array is in range [1, 20,000].
The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].

思路:

感觉这个跟已知数组,求数组区间和那个很类似,[leetcode-303-Range Sum Query - Immutable]

这个是:已知区间和为k,求一共有多少个区间的和为k。

可以通过累积和数组sums来统计数组nums当前元素nums[i]之前所有元素的和,

比如:k= 3

nums: [1, -1, 5, -2, 3], 则得到

sums: [1, 0, 5, 3, 6]

我们可以看到累积和sums的第四个数字为3,则说明前四个数字就是符合题意的一个子数组,再来看第二个例子:

nums: [-2, -1, 2, 1], k = 1

sums: [-2, -3, -1, 0]

可以在nums里找到 -1,2 的和为1,又nums[0]+nums[1]+nums[2] = sums[0]+ nums[1]+nums[2] =  sums[2] 。

则sums[0]+k = sums[2] ,区间和k = sums[2] - sums[0] = (-1) - (-2),

sums[2] - k =sums[0],说明存在区间nums[1],nums[2]的和为k。

然后,再用一个哈希表来建立累积和 与 其坐标之间的映射。如果 H[ sum[i] - k ]>0 则说明存在区间和为k。

int subarraySum(vector<int>& nums, int k)
{
int n = nums.size();
vector<int >sum(n+);//记录累加和
for(int i =;i<=n;i++)
{
sum[i] = sum[i-]+nums[i-];
}
map<int ,int > H;
H[] = ;
int ret = ;
for(int i =;i<=n;i++)
{
ret += H[ sum[i] - k ];
H[sum[i]]++;
}
return ret;
}

参考:

http://www.cnblogs.com/grandyang/p/5336668.html

[leetcode-560-Subarray Sum Equals K]的更多相关文章

  1. leetcode 560. Subarray Sum Equals K 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)

    整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...

  2. [LeetCode] 560. Subarray Sum Equals K 子数组和为K

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  3. LeetCode 560. Subarray Sum Equals K (子数组之和等于K)

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  4. [leetcode]560. Subarray Sum Equals K 和为K的子数组

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  5. 【LeetCode】560. Subarray Sum Equals K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. [LeetCode] 560. Subarray Sum Equals K_Medium

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  7. 560. Subarray Sum Equals K 求和为k的子数组个数

    [抄题]: Given an array of integers and an integer k, you need to find the total number of continuous s ...

  8. 560. Subarray Sum Equals K

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  9. 【leetcode】560. Subarray Sum Equals K

    题目如下:解题思路:本题的关键在于题目限定了是连续的数组,我们用一个dp数组保存第i位到数组末位的和.例如nums = [1,1,1],那么dp = [3,2,1], dp[i]表示nums[i]+n ...

  10. [LeetCode] 325. Maximum Size Subarray Sum Equals k 和等于k的最长子数组

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

随机推荐

  1. 浅论Javascript在汽车信号测试中的应用

    起因 上周老板又给了我这个车辆工程毕业的码农一份工作: 要我写一个测试台架出来. 我先简单的分析了测试台架的几种典型的工况: 1.发送一个CAN信号,测试能否查到. 2.发送一个信号,是否能在规定时间 ...

  2. Java中的Classpath

    classpath实际上就是编译后的,以classes文件夹为起点的路径各种path获取到的路径的区别 Demo.class.getResource("");//得到的是Demo类 ...

  3. nginx-tomcat-memcached架构文档说明(转)

    800x600 Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 st1\:*{be ...

  4. html或者php中 input框限制只能输入正整数,逻辑与和或运算

    有时需要限制文本框输入内容的类型,本节分享下正则表达式限制文本框只能输入数字.小数点.英文字母.汉字等代码. 例如,输入大于0的正整数 代码如下: <input onkeyup="if ...

  5. 一天搞定CSS(扩展):CSS Hack

    做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况.基于此,某些情况我们会极不情愿的使用这个不太友好的方式来达到大家要求的页面表现.我个人是不太推荐使用hack的,要知道 ...

  6. TreeMap集合特点、排序原理

    TreeMap特点(类似于TreeSet): 1.无序,不允许重复(无序指元素顺序与添加顺序不一致) 2.TreeMap集合默认会对键进行排序,所以键必须实现自然排序和定制排序中的一种 3..底层使用 ...

  7. 定期清空log文件

    # auto-del-log.sh #!/bin/shfor i in `find . -name "*.out" -o -name "*.log"`do  c ...

  8. Elasticsearch VS Solr

    最近公司用到了ES搜索引擎,调研发现大公司常用的搜索引擎还有Solr. 鉴于 Lucene 强大的特性和稳定性,有很多种基于 Lucene 封装的企业级搜索平台.其中最流行有两个:Apache Sol ...

  9. Android 图片加载框架Glide4.0源码完全解析(一)

    写在之前 上一篇博文写的是Picasso基本使用和源码完全解析,Picasso的源码阅读起来还是很顺畅的,然后就想到Glide框架,网上大家也都推荐使用这个框架用来加载图片,正好我目前的写作目标也是分 ...

  10. ssh代理上网

    背景: 公司开发机没有外网,但可以通过ssh连接到另一台可以上公网的机器,所以想通过ssh代理的方式上网,简单又方便,而且需要的时候上,不需要的时候也可以不上 配置: 超级简单 在开发机上建立ssh隧 ...