[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 ...
随机推荐
- 阿里云启用IPV6
ping过别人的IPv6网址之后,可以确定,局域网是不支持IPv6的.所以要使用隧道技术建立两台机器之间的IPv6连接 1.发现测试用服务器上没有IPv6地址.所以测试服务器的内核应该是没有IPv6模 ...
- PLSQL导出表结构
1:进行plsql后选怎Tools--------->Exports User Ojbects------------->选中需要导出的table,squence,view,type,fu ...
- python Web开发你要理解的WSGI & uwsgi详解
原文:https://www.jb51.net/article/144852.htm WSGI协议 首先弄清下面几个概念: WSGI:全称是Web Server Gateway Interface,W ...
- django的用户认证组件
DataSource:https://www.cnblogs.com/yuanchenqi/articles/9064397.html 代码总结: 用户认证组件: 功能:用session记录登录验证状 ...
- 常用数据库1 sqlserver
知识内容: 1.SqlServer数据库概述 2.SqlServer数据库基本操作 3.Transact-SQL程序设计 一.SqlServer数据库概述 1.SqlServer系统概述 SQLSer ...
- mysql 定时执行
mysqltablevariables任务sqlinsert 重新定义mysql命令行结束符为//,命令行创建存储过程需要. delimiter // MySQL5.1.x版本中引入了一项新特 ...
- 【独家】完美解决appium安装app时,需要手动确认安装的问题
appium初始化driver时,如果未安装该app会先进行安装,安装时,很多安卓手机都会弹框,需要手动确认安装. 如小米的机器, 这是个头疼的问题,之前在网上找遍了,只有通过adb去点相对坐标成功了 ...
- IdUDPServer中文汉字乱码 及IdTCPClient
官网 http://www.indyproject.org/docsite/html/frames.html?frmname=topic&frmfile=TIdTCPServer_OnExec ...
- ABAP-TXT文件上传
at selection-screen on value-request for pc_file. call function 'WS_FILENAME_GET' exporting ...
- MVC控制器详解
原文地址:http://www.cnblogs.com/SeeYouBug/p/6441934.html#3628606 目录 一.理解控制器 1.1.什么是控制器 1.2.控制器的作用 1.3.创建 ...