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

题目

和为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

代码:

 public class Solution {
public int subarraySum(int[] nums, int k) {
int sum = 0, result = 0;
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 1); for (int i = 0; i < nums.length; i++) {
sum += nums[i];
if (map.containsKey(sum - k)) {
result += map.get(sum - k);
}
map.put(sum, map.getOrDefault(sum, 0) + 1);
} return result;
}
}

[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. [jni]Getting Started

    写一个java应用程序来调用C函数打印“Hello World!" 这个过程包括以下步骤: 1:创建一个申明了native方法的java类(HelloWorld.java): 2:使用jav ...

  2. CSS属性 table 的 border-collapse 边框合并

    说明 该CSS属性用来设定表格的行和列的边框是合并成单边框,还是分别有各自的边框 separate 缺省值.边框分开,不合并.collapse 边框合并.即如果相邻,则共用同一个边框. 虽然在DIV+ ...

  3. Solr学习之二-Solr基础知识

    一 基本说明 简单来说Solr是基于Lucene的高性能的,开源的Java企业搜索服务器.Solr可以看作一个Web app,运行在tomcat或Jetty这类HTTP服务器上, 底层是一个基于Luc ...

  4. python之路之迭代器与生成器

    一  迭代器 那么在研究迭代器之前首先应该要知道什么是迭代. 迭代:是一个重复的过程,并且每次重复都是建立基于上一次的结果而来的,所以在迭代的过程其实是在不断变化的. 迭代器:就是迭代取值的工具. 那 ...

  5. json 拖拽

    1.梳理知识点 1.事件对象   e || event  2.事件对象的属性      鼠标事件对象 : 坐标属性 :  clientX  clientY  pageX  pageY   offset ...

  6. VC如何得到一个文件夹的路径

    VC中没有现成的函数来选择一个文件夹,但这是经常会用到的,怎么办?自动动手,丰衣足食! 使用SHBrowseForFolder,代码如下: #include   int SelFolder(HWND ...

  7. uva-10152-乌龟排序

    求从待排序的到期望的顺序的最小操作顺序,只能进行一个操作,将当前的乌龟拿出来,上面的下移,拿出来的放到最上面 发现voj没有PE, 解题方法,把俩个串反过来使用,从期望的顺序到待排序的顺序. AC:1 ...

  8. viewer 照片查看器

    viewer 照片查看器 效果: api: https://github.com/fengyuanchen/viewerjs#methods npm: npm install viewerjs 使用: ...

  9. XCode iOS Simulator 模拟器

    XCode7.3下,默认带了iOS 9.3 Simulator,iOS 8.4 Simulator总是安装不成功. mac os X,里的模拟器,全屏 ,windows win键+1/2/3 切换全屏 ...

  10. word自动生成章节标题

    一级目录 二级目录 三级标题