560. Subarray Sum Equals K 求和为k的子数组个数

时间:2023-01-23 13:31:06

[抄题]:

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

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 统计次数的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的子数组个数的更多相关文章

  1. leetcode 560&period; Subarray Sum Equals K 、523&period; Continuous Subarray Sum、 325&period;Maximum Size Subarray Sum Equals k&lpar;lintcode 911&rpar;

    整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...

  2. &lbrack;LeetCode&rsqb; 560&period; Subarray Sum Equals K 子数组和为K

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  3. &lbrack;leetcode&rsqb;560&period; Subarray Sum Equals K 和为K的子数组

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  4. 【LeetCode】560&period; Subarray Sum Equals K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. LeetCode 560&period; Subarray Sum Equals K (子数组之和等于K)

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  6. 560&period; Subarray Sum Equals K

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  7. &lbrack;LeetCode&rsqb; 560&period; Subarray Sum Equals K&lowbar;Medium

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  8. 【leetcode】560&period; Subarray Sum Equals K

    题目如下:解题思路:本题的关键在于题目限定了是连续的数组,我们用一个dp数组保存第i位到数组末位的和.例如nums = [1,1,1],那么dp = [3,2,1], dp[i]表示nums[i]+n ...

  9. &lbrack;LeetCode&rsqb; 325&period; 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 ...

随机推荐

  1. 【poj2449】 Remmarguts&&num;39&semi; Date

    http://poj.org/problem?id=2449 (题目链接) 题意 求有向图K短路. Solution A*.g(x)为当前节点到起点的步数,h(x)为当前节点到终点的最短距离(也就是估 ...

  2. 关于成为Java高级工程师之路

    简单说明一下现状,个人目前学习使用java已经一年半,很迷茫,高不成低不就,在此列一个目标,为期18个月,再来个一年半,这样软件生涯三年后,我必须成为高级工程师! 这里涉及Java各个方面的知识,有的 ...

  3. 【 2013 Multi-University Training Contest 8 】

    HDU 4678 Mine 对于每个空白区域,求SG值. 最后异或起来等于0,先手必败. #pragma comment(linker,"/STACK:102400000,102400000 ...

  4. oracle表的管理

    表名和列的命名规则 必须以字母开头: 长度不能超过30字符: 不能使用oracle的保留字: 只能使用如下字符:A-Z,a-z,0-9,$,#等:   数据类型: 字符型: char       定长 ...

  5. Ubuntu 安装WPS

    1.到官网下载deb安装包 http://community.wps.cn/download/ 2.安装 sudo dpkg -i wps-office_10.1.0.5672~a21_amd64.d ...

  6. HDU 5929 Basic Data Structure 模拟

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  7. Slave延迟很大的优化方法总结(MySQL优化)

    [http://www.cstor.cn/textdetail_9146.html] 一般而言,slave相对master延迟较大,其根本原因就是slave上的复制线程没办法真正做到并发.简单说,在m ...

  8. Spring Framework 中启动 Redis 事务操作

    背景: 项目中遇到有一系列对Redis的操作,并需要保持事务处理. 环境: Spring version 4.1.8.RELEASE Redis Server 2.6.12 (64位) spring- ...

  9. 怎么给没链接的flash加超链接

    最近开始准备设计一个广告条,本想用阿里妈妈的的banner marker来设计,却遗憾的发现,banner marker已经实行收费模式了. 我不得不启用另一款在线banner生成工具,百度旗下的&q ...

  10. hdu 2044 递推

    到达第n个格子的方案数等于第n-1个格子的方案数加上第n-2个格子的方案数. d[i]=d[i-1]+d[i-2]; AC代码: #include<cstdio> const int ma ...