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 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].
题目标签:Array, Map
题目给了我们一个nums array 和一个 k, 要我们找出有多少个子数组 之和是等于 k的。
一开始想到的是暴力解法,虽然能够通过,但是n*n 始终是太慢。
这题可以利用HashMap,把出现过的sum 当作key 存入, 把这个sum 出现过的次数 当作value 存入。
遍历nums array,一直更新sum,然后去map 里找有没有 sum - k,有的话说明 sum - k 是一个旧的sum,之前出现过。换句话说,新的sum - 旧的sum = k,说明 新的sum 减去 旧的sum,剩下的那一段的 和 等于k, 找到了一个子数组之和 = k的。然后在把新的sum 存入 map里。
这题的关键就是,当我们知道了一个 sum(0,i) 和 sum(0,j)的话,我们也就知道了 sum(i+1, j)。
所以当我们知道了目前所有旧的sum, 当我们有了一个新的sum,我们可以利用map 去找到 新的sum - k 是不是在map里存在。一旦存在,说明了我们找到了一个 子数组它的和为k。
要注意的是,这里count +的是map 里的value,而不是count++, 因为当你在map 里找到了一个 sum - k的旧sum的时候,这个旧的sum 可能出现过2次,换句话说,可能有两个长度不一样的子数组,但是它们的和都等于 sum - k(因为有负数)。所以这里要加上旧sum 的出现次数2,因为你找到了两个不同的子数组它们的和都为k。
Java Solution:
Runtime beats 67.25%
完成日期:10/03/2017
关键词:Array, HashMap
关键点:把所有出现过的sum存入map:sum为key,出现的次数为value;利用map来找k (新sum - k =? 任何旧sum)
class Solution
{
public int subarraySum(int[] nums, int k)
{
int count = 0;
int sum = 0; Map<Integer, Integer> map = new HashMap<>();
map.put(0, 1); // initial value sum = 0, occurrence = 1 for case sum = k, k - k = 0 counts for(int i=0; i<nums.length; i++) // iterate nums array
{
sum += nums[i]; // update sum if(map.containsKey(sum - k)) // if map has sum - k, meaning this new sum - old sum = k
count += map.get(sum - k); // previous sum might appear more than once
// this is why we need to add its value
map.put(sum, map.getOrDefault(sum, 0) + 1); // save new sum into map
} return count;
}
}
参考资料:
https://discuss.leetcode.com/topic/87850/java-solution-presum-hashmap
LeetCode 题目列表 - LeetCode Questions List
LeetCode 560. Subarray Sum Equals K (子数组之和等于K)的更多相关文章
- [LeetCode] Continuous Subarray Sum 连续的子数组之和
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- 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_Medium
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- 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] 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] Subarray Product Less Than K 子数组乘积小于K
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- 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 ...
随机推荐
- JDBC第四篇--【数据库连接池、DbUtils框架、分页】
1.数据库连接池 什么是数据库连接池 简单来说:数据库连接池就是提供连接的. 为什么我们要使用数据库连接池 数据库的连接的建立和关闭是非常消耗资源的 频繁地打开.关闭连接造成系统性能低下 编写连接池 ...
- 【机器学习实战】Machine Learning in Action 代码 视频 项目案例
MachineLearning 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远 Machine Learning in Action (机器学习实战) | ApacheCN(apa ...
- cnblogs第一天
2017-08-25 21:27:16 今天算是真的下定决心要好好的去经营自己的博客了. flag也就不立了,毕竟,flag这种东西立了就是为了打脸的嘛....... 既然说是经营了,那必然是要认认真 ...
- 前端系列——jquery前端国际化解决方案“填坑日记”
前言:最近,新的平台还没有开发完成,原来的老项目又提出了新的需求:系统国际化.如果是前后端完全分离的开发模式,要做国际化,真的太简单了,有现成的解决方案,基于Node构建的时下热门的任何一种技术选型都 ...
- kbhit()
kbhit() 非阻塞的响应键盘输入时间 C++函数 功能和返回值:检查是否有键盘输入 ,有返回非0 ,无返回0 int khbit(void) 头文件: #include<conio.h& ...
- HDFS概述(2)————Block块大小设置
以下内容转自:http://blog.csdn.net/samhacker/article/details/23089157?utm_source=tuicool&utm_medium=ref ...
- HDU 1754 I Hate It(线段树区间求最值)
很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老师有 ...
- wireshark数据包分析实战 第二章
1,监听网络线路:即嗅探器的位置确定. 2,混杂模式:将网卡设置成混杂模式,网卡可以接受经过网卡的所有数据报,包括目的地址不是本网卡的数据报.这些数据都会发送给cpu处理,这样,wireshark就能 ...
- springMVC中的redirect和forward区别?
1.forward在跳转后可以取到message值,redirect在跳转后无法取到message值. 2.forward跳转后地址栏URL不会改变,而redirect会改变.
- java课程作业--动手动脑
随机数: 1)编写一个方法,使用以下算法生成指定数目(比如1000个)的随机整数. Modulus=231-1=int.MaxValue Multiplier=75=16807 C=0 当显示过231 ...