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 ...
随机推荐
- logback:logback和slf4j中的:appender、logger、encoder、layout
(1)appender 1.appender标签是logback配置文件中重要的组件之一.在logback配置文件中使用appender标签进行定义.可 以包含0个或多个appender标签. 2.a ...
- Oracle 修改序列的初始值
Oracle 序列(Sequence)主要用于生成主键.但是,有时需要修改序列初始值(START WITH)时,好多人凭感觉认为:Alter Sequence SequenceName Start W ...
- OSGi-开发环境的建立和HelloWorld(04)
1 OSGi开发环境的建立 1.1 Equinox是什么 从代码角度来看,Equinox其实就是OSGi核心标准的完整实现,并且还在这个基础上增加了一些额外的功能(比如为框架增加了命令行和程序执行的入 ...
- mysql数据库-注释相关介绍
mysql执行的sql脚本中注释怎么写? mysql 服务器支持 # 到该行结束.-- 到该行结束 以及 /* 行中间或多个行 */ 的注释方格: mysql; # 这个注释直到该行结束 mysql; ...
- 用vue开发一个app(2,main.js)
昨天跟着vue的官网搭建了vue的一个脚手架,我也是第一次用VUE一切都在摸索阶段. 今天试着看下里面脚手架里面有点什么东西 先看看main.js 导入了3个模块 一个vue,一个app,还有rout ...
- H5页面解决IOS进入不自动播放问题(微信内)
废话少说,直接上代码. 主要还是调用微信的jdk做兼容处理.,安卓可自动播放. ($(function(){ $(function(){ /* ** 复选框*/ $('.ul-radio').on(' ...
- bzoj3624(铺黑白路)(并查集维护)
题意网上自己随便找,绝对是找的到的. 题解:(白边表示鹅卵石路,黑边表示水泥路)这道题的解法,先考虑将黑边所有都先连起来,组成一个又一个的联通块,然后用白边去连, 如果可以联通的话,就用白边去代替黑边 ...
- 关于如何获取iframe中的元素
今天研究了一下iframe中元素的获取,发现有些地方还是有点坑的. 首先:如果使用纯前端手段,是没有办法获取非同源的iframe中的元素的,后面会提到后端手段 一.同源环境 1.首先在父页面获取ifr ...
- Vue使用总结
好久没更新博客,确实是自己已经懒癌晚期,最近毕业刚工作3个月,公司开发一直在用Vue,自己个人也比较喜欢这个框架,今天就对自己学习到和用到的知识点作一些总结,希望能帮到大家. Vue 知道Vue也一定 ...
- 使用路由延迟加载 Angular 模块
使用路由延迟加载 Angular 模块 Angular 非常模块化,模块化的一个非常有用的特性就是模块作为延迟加载点.延迟加载意味着可以在后台加载一个模块和其包含的所有组件等资源.这样 Angular ...