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
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
没思路啊。以为要用sliding window:求和类问题可以往2sum上面靠。
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
连续求和为k,可以用连续的sum减去k
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 统计次数的map必须要有初始化
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
连续求和为k,可以用连续的sum减去k
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public int subarraySum(int[] nums, int k) {
//initialization: hashmap, preSum, count
HashMap<Integer, Integer> sumToCount = new HashMap<Integer, Integer>();
int preSum = 0;
int counts = 0;
sumToCount.put(0, 1);
for (int i = 0; i < nums.length; i++) {
preSum += nums[i];
//if already contains, += get(sum - k)
if (sumToCount.containsKey(preSum - k)) {
counts += sumToCount.get(preSum - k);
}
//or just add the key
sumToCount.put(preSum, sumToCount.getOrDefault(preSum, 0) + 1);
}
//return
return counts;
}
}
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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- 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_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
题目如下:解题思路:本题的关键在于题目限定了是连续的数组,我们用一个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 ...
随机推荐
- 单源最短路径Dijkstra算法,多源最短路径Floyd算法
1.单源最短路径 (1)无权图的单源最短路径 /*无权单源最短路径*/ void UnWeighted(LGraph Graph, Vertex S) { std::queue<Vertex&g ...
- 论文阅读笔记:【MDNet】
[MDNET]: H Nam, B Han. Learning multi-domain convolutional neural networks for visual tracking[C]. / ...
- forward reference前向引用,gloal values and local values全局变量和局部变量,recursive function递归函数
1.全局变量与局部变量练习 1 # -*- coding: UTF-8 -*- 2 def bar(): 3 print('from bar') 4 def foo(): 5 print('from ...
- 图的遍历——DFS(矩形空间)
首先,这里的图不是指的我们一般所说的图结构,而是大小为M*N的矩形区域(也可以看成是一个矩阵).而关于矩形区域的遍历问题经常出现,如“寻找矩阵中的路径”.“找到矩形区域的某个特殊点”等等之类的题目,在 ...
- 报错:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1
错误现象: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-com ...
- python:数据类型dict
一.字典 key -->value 储存大量数据,而且是关系型数据,查询速度非常快 数据类型分类: 可变数据类型:list , dict, set 不可变的数据类型:int , bool, st ...
- Unity Shader Graph(二)Dissolve Effect
此篇文章记录Dissolve Effect(溶解特效)的制作过程 软件环境 Unity 2018.1.2f1 Packages: Lightweight Render Pipeline 1.1.11 ...
- Includes() vs indexOf() in JavaScript
碰到一个问题, 部分机器网页数据源不正常, 简单排查发现是使用了较新的Array.includs 方法. 查了下兼容性, chrome 需要47版本以后支持, 客户机果然是很久的43版本. 用Arra ...
- Linux背背背(2)
目录: 1.简单命令 2.目录切换命令 3.扩展命令 简单命令 ls 语法1:#ls [路径] 表示列出指定路径下的文件夹和文件的名字,如果路径没有指定则列出当前路径下的 语法2 ...
- postgresql分区(引用)
1 建立大表. 2 创建分区继承 3 定义Rule或者Trigger? 1 建立大表 CREATE TABLE student (student_id bigserial, na ...