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. 单例模式(Singleton)看了就懂

    单例,故名思议,一个只能创建一个实例的类. 单例被广泛应用于Spring的bean(默认).线程池.数据库连接池.缓存,还有其他一些无状态的类如servlet. 一个没必要多例的类实现了单例可以节约空 ...

  2. XML功能

    REF:https://www.baidu.com/link?url=_-UY8rZVAORlesKTth0F7C8LbvXCL4lSwz6vmQVnTEgmT06NFGdoaD9FbuEQhR7xG ...

  3. C#抓取数据、正则表达式+线程池初步运用

    去年底用 多线程+HtmlAgilityPack.dll 写了一个抓取“慧聪网” 公司信息的小程序,代码惨不忍赌.好在能抓到数据,速度也能让人忍受就很久没管了. 最近这段时间把这个小程序发给同事看着玩 ...

  4. WebSocket In ASP.NET Core(二)

    WebSocket In ASP.NET Core(二) Server in ASP.NET-Core DI in ASP.NET-Core Routing in ASP.NET-Core Error ...

  5. 我的Spring学习记录(二)

    本篇就简单的说一下Bean的装配和AOP 本篇的项目是在上一篇我的Spring学习记录(一) 中项目的基础上进行开发的 1. 使用setter方法和构造方法装配Bean 1.1 前期准备 使用sett ...

  6. OC——多态

    书接上文,上文提到继承一个很大用途的是为了更好的实现多态,现在我们就来看看OC的多态. 多态:顾名思义就是好多种状态,以前学C#时候印象最深刻的例子是好多个类共同实现同一个接口,然后把这些类的对象都装 ...

  7. oracle数据库知识点

    1.oracle启动后的服务 1. Oracle ORCL VSS Writer Service:Oracle卷映射拷贝写入服务,VSS(Volume Shadow Copy Service)能够让存 ...

  8. JSP入门 Filter

    Filter,它的名字是过滤器,可以批量拦截修改servlet的请求和响应. 需要实现接口Filter 配置 <filter> <filter-name>EncodingFil ...

  9. Python文件读写模式

    r 打开只读文件,该文件必须存在. r+ 打开可读写的文件,该文件必须存在.可读,可写,可追加. w 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失.若文件不存在则建立该文件. w+ 打 ...

  10. OpenCV探索之路(二十六):如何去除票据上的印章

    最近在做票据的识别的编码工作时遇到一些问题,就是票据上往往会有一些红色印章把一些重要信息区域给覆盖了,比如一些开发票人员盖印章时比较随意,容易吧一些关键区域给遮蔽了,这让接下来的票据识别很困难,因此, ...