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 ...
随机推荐
- JS严格模式
如何开启严格模式? 在js中,只需要在顶部添加"use strict",即可进入严格模式 在函数中加上"use strict"编辑指示,也可以指定函数在严格模式 ...
- [03] Servlet继承关系和生命周期
1.Servlet的继承关系 假如现有我们自定义的一个Servlet,继承HttpServlet,那么实际上它的继承链如下图: 可以看到,核心的部分在于: 两个顶级接口 Servlet Servl ...
- 手机管家iPhoneX的适配总结
WeTest 导读 随着苹果发布会的结束,Xcode的GM版也上线了,也意味着iPhoneX适配之旅的开始. 一.设计关注篇 注意设计的基本原则:(苹果呼吁的) 规格原帖:https://develo ...
- RabbitMQ消息队列之一:RabbitMQ的环境安装及配置
RabbitMQ简介: MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们 ...
- Redis学习笔记之一 : 配置redis
Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久 ...
- Angular2响应式表单
本文将半翻译半总结的讲讲ng2官网的另一个未翻译高级教程页面. 原文地址. 文章目的是使用ng2提供的响应式表单技术快速搭出功能完善丰富的界面表单组件. 响应式表单是一项响应式风格的ng2技术,本文将 ...
- S2_SQL_第四章
1.使用EXISTS语句判断该数据库对象是否存在的语法: DROP TABLE IF EXISTS temp; 2. EXISTS作为WHERE语句的子查询: SELECT <字段>FRO ...
- codeforces 8c Looking for Order
https://vjudge.net/problem/CodeForces-8C 题意: 一个平面上放着许多东西,每个东西都有一个坐标,最开始一个人在一个起始坐标,她出发去拿东西,一次要么拿一件东西, ...
- cnpm的全局安装
npm install -g cnpm --registry=https://registry.npm.taobao.org
- 入坑IT十年(二)技术以外
上一篇博客里提到:技术越来越简单,发布后不久,就看到<技术并不是越来越简单>,这显然是打擂台来了. 技术究竟是不是越来越简单?其实这个问题,要看你究竟是以什么角度来思考这个问题.我们可以举 ...