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:

  1. The length of the array is in range [1, 20,000].
  2. 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)的更多相关文章

  1. [LeetCode] Continuous Subarray Sum 连续的子数组之和

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

  2. 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题是存储的当前累加和的 ...

  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_Medium

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

  6. 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 ...

  7. [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 ...

  8. [LeetCode] Subarray Product Less Than K 子数组乘积小于K

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  9. 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 ...

随机推荐

  1. 软件测试的cookie测试

    1.什么是cooike测试 Cookie是指网站用于辨别身份,进行会话(session)跟踪而存储在客户端的数据.它是有服务器产生并发送给客户端的.其用途是提供一个方便的功能以简化用户输入,节省访问页 ...

  2. 离线安装 Cloudera Manager 5 和 CDH5.10

    关于CDH和Cloudera Manager CDH (Cloudera's Distribution, including Apache Hadoop),是Hadoop众多分支中的一种,由Cloud ...

  3. PeopleRank

    PeopleRank:基于PageRank的理论,以每个微博账户的“关注”为链出链接,“粉丝”为链入链接的这种以人为核心的关系. PeopleRank假设条件:– 数量假设:如果一个用户节点接收到的其 ...

  4. 使用cocos2d脚本生成lua绑定

    这几天要老大要求把DragonBones移到cocos2dx 3.0 里边,并且绑定lua使用接口.因为刚学lua,使用的引擎也刚从2.2改为3.0,各种不熟悉,折腾了好几天才弄完,有空了总结一下 这 ...

  5. angular 如何获取使用filter过滤后的ng-repeat的数据长度

    在做项目的过程中,被产品要求在内容为空的过程中显示提示信息,然哦户内容使用ng-repeat循环输出的,并且使用了filter过滤.后在谷歌上找到解决方案,如下: ​之前代码如下显示: <ul& ...

  6. Spring 学习——基于Spring WebSocket 和STOMP实现简单的聊天功能

    本篇主要讲解如何使用Spring websocket 和STOMP搭建一个简单的聊天功能项目,里面使用到的技术,如websocket和STOMP等会简单介绍,不会太深,如果对相关介绍不是很了解的,请自 ...

  7. Vi快捷操作 vim配置【shell文件格式从windows转换为linux】

    vim配置 http://www.cnblogs.com/ma6174/archive/2011/12/10/2283393.html gg 首行 dd 删除当前行 :.,$d  删除全部内容 :se ...

  8. Windows下如何创建低权限进程

       1.  前言 在使用 Sysinternals 出品的 Process Explorer 过程中,对 “Run as Limited User” 功能的实现方式颇感兴趣,一番搜寻之下发现Mark ...

  9. 为什么ABAP开发者需要使用面向对象技术?

    ABAP对面向对象的支持已有十多年的历史,然而在生产实践中,我们对这门技术的应用十分有限. 一方面,面向过程的惯性长期存在着:另一方面,对于大部分二次开发工作而言,似乎并没有足够的理由促使开发者使用面 ...

  10. Linux下Apache https认证

    参考:http://kyfxbl.iteye.com/blog/1910891 http://showerlee.blog.51cto.com/2047005/1266712 一.环境 httpd:A ...