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:

  1. Input:nums = [1,1,1], k = 2
  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].

题目

和为K的子数组

思路:

1. The key to solve this problem is to find subarray sum[i, j] == k

2. if we know sum[0, i-1], sum[0,j]   we can check sum[0, i-1] - k == sum[0,j] ?

3. To achieve this, we traversal whole array, save sum[0,j] as preSum in map, checking curSum[0,i-1] - k is contained in map

nums = [5,   2,   3,   4,   5]      k= 7

sum=5

=7

=10

=14

map

sum    frequency             check (sum - k) in map?

init        0              1

5               1                           5-7=-2   No

7               1                           7-7=0    Yes   update res

10              1                           10-7=3   No

14              1                           14-7=7  Yes   update res

代码:

  1. public class Solution {
  2. public int subarraySum(int[] nums, int k) {
  3. int sum = 0, result = 0;
  4. Map<Integer, Integer> map = new HashMap<>();
  5. map.put(0, 1);
  6.  
  7. for (int i = 0; i < nums.length; i++) {
  8. sum += nums[i];
  9. if (map.containsKey(sum - k)) {
  10. result += map.get(sum - k);
  11. }
  12. map.put(sum, map.getOrDefault(sum, 0) + 1);
  13. }
  14.  
  15. return result;
  16. }
  17. }

[leetcode]560. Subarray Sum Equals K 和为K的子数组的更多相关文章

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

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

  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_Medium

    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 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

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

  7. 560. Subarray Sum Equals K

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

  8. 【leetcode】560. Subarray Sum Equals K

    题目如下:解题思路:本题的关键在于题目限定了是连续的数组,我们用一个dp数组保存第i位到数组末位的和.例如nums = [1,1,1],那么dp = [3,2,1], dp[i]表示nums[i]+n ...

  9. [LeetCode] 325. Maximum Size Subarray Sum Equals k 和等于k的最长子数组

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

随机推荐

  1. 基于标准库实现string和wstring的转换

    // convert string to wstring std::wstring to_wstring(const std::string& str, const std::locale&a ...

  2. PHP的mysqli_query参数MYSQLI_STORE_RESULT和MYSQLI_USE_RESULT的区别

    这篇文章主要介绍了PHP的mysqli_query参数MYSQLI_STORE_RESULT和MYSQLI_USE_RESULT的区别,本文给出了这两个参数的5个区别,需要的朋友可以参考下 虽然nos ...

  3. Web 跨域请求(OCRS) 前端解决方案

    1.同源策略如下: URL 说明 是否允许通信 http://www.a.com/a.jshttp://www.a.com/b.js 同一域名下 允许 http://www.a.com/lab/a.j ...

  4. Redis 集合 set 操作, 有序集合

    01, 唯一性, 确定性, 无序性 ( 结合的三大特性 ) 02, 新建集合, 或者往集合中添加数据 => sadd key value1 value2 value3 ....... 03, 查 ...

  5. leetcode134

    class Solution { public: inline int get_next(int idx, int size) { ? : idx+; } int aux(int idx, vecto ...

  6. angular 使用服务共享数据需要注意

    在使用服务共享数据时,需要注意一些细节,否则会出现视图不刷新,也不报错这样的问题,遇到了,总结下 如下: <div ng-controller='ctr1'> <a href={{n ...

  7. focusin 事件| focusout事件

    focusin 定义和用法 当元素(或在其内的任意元素)获得焦点时发生 focusin 事件. 当在元素或在其内的任意元素上发生 focus 事件时,focusin() 方法添加要运行的函数. 与 f ...

  8. 基于 tensorflow 的 mnist 数据集预测

    1. tensorflow 基本使用方法 2. mnist 数据集简介与预处理 3. 聚类算法模型 4. 使用卷积神经网络进行特征生成 5. 训练网络模型生成结果 how to install ten ...

  9. Boost.Hana

    Boost.Hana Boost.Hana 是一个元编程的库.它为不同种类数据的集合以及类型的集合提供了容器和算法. #include <boost/hana.hpp> namespace ...

  10. JAVA中会存在内存泄露吗

    所谓内存泄露就是指一个不再被程序使用的对象或变量一直被占据在内存中.java中有垃圾回收机制,它可以保证一对象不再被引用的时候,即对象编程了孤儿的时候,对象将自动被垃圾回收器从内存中清除掉.由于Jav ...