[leetcode-560-Subarray Sum Equals K]
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]的更多相关文章
- 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题是存储的当前累加和的 ...
- [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 ...
- 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 ...
- [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 ...
- 【LeetCode】560. Subarray Sum Equals K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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 ...
- 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 ...
- 560. Subarray Sum Equals K
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- 【leetcode】560. Subarray Sum Equals K
题目如下:解题思路:本题的关键在于题目限定了是连续的数组,我们用一个dp数组保存第i位到数组末位的和.例如nums = [1,1,1],那么dp = [3,2,1], dp[i]表示nums[i]+n ...
- [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 ...
随机推荐
- CountDownLacth详解
一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 用给定的计数 初始化 CounDownLatch.由于调用了countDown() 方法,所以在当前计数到达零之 ...
- web 直播&即时聊天------阿里云、融云
随着直播越来越火,所在公司也打算制作自己的直播,所以去了解了这方面,使用后发现还是有些问题需要记录的. 经过分析,制作直播应该是分为两块来做,即直播与实时评论.这里先去制作实时评论,等直播ok后,也会 ...
- 用kotlin方式打开《第一行代码:Android》
参考:<第一行代码:Android>第2版--郭霖 注1:本文为原创,例子可参考郭前辈著作:<第一行代码:Android> 注2:本文不赘述android开发的基本理论,不介绍 ...
- 如何用C#完成控制台日历?
本题目的最终要就是根据用户输入的年和月在控制台输出单月的日历信息,附加范围年在1900-2100之间,月的范围在1-12之间,当用户输入不在范围时要给予错误信息提示:已知条件是1900年1月1日为星期 ...
- 单行 JS 实现移动端金钱格式的输入规则
金钱格式检验属于很普通的需求,记得工作中第一次遇到这个需求的时候,还不太会写正则表达式,搜到了一个类似的解决方案,看着正则的文档改成了自己需要的形式. 但是用户的输入操作是任意的,只是显示提示信息,这 ...
- $.when()方法翻译
地址:http://api.jquery.com/jQuery.when/ jQuery.when( deferreds ),returns Promise 正文 Description: Provi ...
- 数据挖掘应用案例:RFM模型分析与客户细分(转)
正好刚帮某电信行业完成一个数据挖掘工作,其中的RFM模型还是有一定代表性,就再把数据挖掘RFM模型的建模思路细节与大家分享一下吧!手机充值业务是一项主要电信业务形式,客户的充值行为记录正好满足RFM模 ...
- 关于python编译的一点小结
大家都知道python是脚本语言,源码可以直接执行,有时需要提高执行效率或者保密(因为有时候不想让使用人看到源码文件),那就涉及到python编译了,那么该如何做呢? 有两种方法可以做到. 1.一种是 ...
- python str的一些方法
在python有各种各样的string操作函数.在历史上string类在python中经历了一段轮回的历史.在最开始的时候,python有一个专门的string的module,要使用string的方法 ...
- 图表(Chart & Graph)你真的用对了吗?
欢迎大家持续关注葡萄城控件技术团队博客,更多更好的原创文章尽在这里~~ 工作中,我们常常会遇到各式各样的数据,例如网站性能,销售业绩,客户服务 .营销活动等数据.对于这些数据,有哪些行之有效的方法来形 ...