[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].
题目
和为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的子数组的更多相关文章
- 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】560. Subarray Sum Equals K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- 560. Subarray Sum Equals 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
题目如下:解题思路:本题的关键在于题目限定了是连续的数组,我们用一个dp数组保存第i位到数组末位的和.例如nums = [1,1,1],那么dp = [3,2,1], dp[i]表示nums[i]+n ...
- [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 ...
随机推荐
- php表达式
表达式是PHP中一个重要的概念,可以把表达式理解为“任何有值的东西”.在本教程中涉及到表达式的语法,我们以“expr”来表示表达式. 下面就是一个表达式: $x > $y; 在上面的例子中,当$ ...
- Spark交互式工具spark-shell
REPL Spark REPL Spark shell 下面我们启动一下(我这里搭建的是3节点集群) sc.后面按TAB键可以把提示调出来 查看hdfs上文件内容 这个数据从这里下载的 https:/ ...
- RAID5---块,条带,校验,旋转,同步/异
冗余(奇偶校验)块:RAID5中在同一个条带中用一个块来存放冗余信息,冗余信息示其他块的"异或"值,这样在同一条带中就只有(n-1)个块是实际的数据,所以RAID5中阵列容量是(n ...
- solr 通过【配置、多值字段、动态字段】来解决文本表达式查询精确到句子的问题
一.Solr Multivalue field属性positionIncrementGap理解 分类:Lucene 2014-01-22 10:39阅读(3596)评论(0) 参考:http://ro ...
- CSS3基础知识(一)
结构选择器 :nth-child(n) 第几个元素 从1开始设置 :nth-child(2n) 偶数元素 从0开始设置 :nth-child(2n+1) 奇数元素 :nth-of-type(n) :f ...
- EF中GroupBy扩展方法的简单使用
public ActionResult ShopInfo() { ViewBag.ShopList = ShopService.GetEntities(x => x.IsDelete == fa ...
- linux系统下修改文件夹目录权限-chmod
Linux.Fedora.Ubuntu修改文件.文件夹权限的方法差不多.很多人开始接触Linux时都很头痛Linux的文件权限问题.这里告诉大家如何修改Linux文件-文件夹权限.以主文件夹下的一个名 ...
- c# 导入导出excel表格式
c#使用代码导入excel时,当遇到纯数字且大于15位时会出现编码混乱(表现为科学计数法),要想呈现与excel表中纯数字格式和在数据库中呈现纯数字,操作如下: 完成即可. 导出取决于导入的内容排版.
- python读取excel,数字都是浮点型,日期格式是数字的解决办法
excel文件内容: 读取excel: # coding=utf-8 import xlrd import sys reload(sys) sys.setdefaultencoding('utf-8' ...
- icil 参考docker
@All 有不知道怎么用docker发布项目的,请参考 http://192.168.18.224:8888/svn/Enterprise/site/docker/overview of docker ...