[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 ...
随机推荐
- JQUERY dialog的用法详细解析
本篇文章主要是对JQUERY中dialog的用法进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助 今天用到了客户端的对话框,把 jQuery UI 中的对话框学习了一下. 准备 jQ ...
- linux下一个启动和监测多个进程的shell脚本程序
#!/bin/sh# Author:tang# Date:2017-09-01 ProcessName=webcrawlerInstanceCount=6RuntimeLog='runtime.log ...
- sklearn.externals import joblib模块保存和下载使用模型的用法实例
#加载模块 from sklearn import datasets from sklearn.externals import joblib from sklearn.linear_model im ...
- Web API源码剖析之HttpServer
Web API源码剖析之HttpServer 上一节我们讲述全局配置.本节将讲述全局配置的DefaultServer,它是一个HttpServer类型. 主要作用就是接受每一次请求,然后分发给消息处理 ...
- Sklearn与特征工程
Scikit-learn与特征工程 “数据决定了机器学习的上限,而算法只是尽可能逼近这个上限”,这句话很好的阐述了数据在机器学习中的重要性.大部分直接拿过来的数据都是特征不明显的.没有经过处理的或者说 ...
- 4. 纯 CSS 创作一个金属光泽 3D 按钮特效
原文地址:https://segmentfault.com/a/1190000014599280 HTML代码: <div class="box">BUTTON< ...
- centos7防火墙使用方法
参考网站:https://blog.csdn.net/achang21/article/details/52538049 添加开放指定端口: [root@yao bin]# firewall-cmd ...
- javaScript日期
日历 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"> <html> <head> <t ...
- SAP订单状态最详细的解释
order status description explanation CRTD 建立 生产订单创建时的状态,表明订单处于刚刚创建时点,不允许做后续发料,确认等操作. PREL 部分释放(部分下达) ...
- Axure8 实现移动端页面上下滑动效果
目前,很多Axure新人都在问如何实现界面上下滑动效果,网上相关的教程也不少,各有各的方法,但是很少有教程对滑动界限设置做出比较详细的说明,其实在工作过程中,个人发现练好Axure是很有意提升逼格的, ...