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 ...
随机推荐
- CH5702 Count The Repetitions
题意 5702 Count The Repetitions 0x50「动态规划」例题 描述 定义 conn(s,n) 为 n 个字符串 s 首尾相接形成的字符串,例如: conn("abc& ...
- VS2015 使用 Visual Studio Emulator For Android 调试无法命中断点的解决办法?
源解决方案是英文版的,地址:https://dzone.com/articles/fix-for-could-not-connect-to-the-debugger-while-de 问题现象: 1. ...
- H2数据库
官网:http://www.h2database.com H2数据库默认的~/test数据库在Win10下所在的位置为 C:/Users/yourname/下,也就是执行cmd的第一个目录 其中的.h ...
- BCC校验小知识
BCC校验其实是奇偶校验的一种,但也是经常使用并且效率较高的一种.所谓BCC校验法,就是在发送前和发送后分别把BCC以前包括ETX字符的所有字符按位异或后,按要求变换(增加或去除一个固定的值)后所得到 ...
- 学习笔记之Machine Learning Crash Course | Google Developers
Machine Learning Crash Course | Google Developers https://developers.google.com/machine-learning/c ...
- gradle重复依赖终极方案解决办法
buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build: ...
- Ubutu16.04+Cuda9.2/9.0+Cudnn7.12/7.05+TensorFlow-gpu-1.8/1.6
目录 Ubuntu16.04 Installl 1. 安装环节 2. 安装卡死 3. NVIDIA显卡安装 2. CUDA Install 3.Cudnn7.05 Install 4.Tensorfl ...
- 20165205 2017-2018-2 《Java程序设计》课程总结
20165205 2017-2018-2<Java程序设计>课程总结 每周作业链接总结 预备作业一: 简述了我认为好的师生关系,展望了未来学习java的日子 预备作业二:总结了C语言的学习 ...
- boost安装缺少libboost_iostreams.so
编译安装boost库: 1 ./bootstrap.sh 2 ./bjam 3 ./b2 install 但安装boosth后,发现缺少libboost_iostreams.so库,后发现boost库 ...
- python基础之从认识python到python的使用
python的历史: python的创始人是吉多·范罗苏姆(Guido van Rossum),人称“龟叔”,1989年圣诞节期间,Guido开始写Python语言的编译器.他希望这个叫做Python ...