[抄题]:

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. Subarray Sum Equals K 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)

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

  2. [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 ...

  3. [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 ...

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

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

  5. 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 ...

  6. 560. Subarray Sum Equals K

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

  7. [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 ...

  8. 【leetcode】560. Subarray Sum Equals K

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

  9. [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 ...

随机推荐

  1. PythonStudy——字典的定义 Dictionary definition

    # 空字典 d1 = {} d2 = dict() # 用map映射创建字典 d3 = dict({'a': 1, 'b': 1}) print(d3) # 用关键字赋值方式 d4 = dict(na ...

  2. BNF

    Backus-Naur Form, 巴科斯-诺尔 范式:一种描述高级语言语法的表示法. BNF 符号概览 符号 描述 ::= 该符号左边的元素被该符号右边的结构所定义 *: 该符号前面的结构可以重复零 ...

  3. 将string转为同名类名,方法名。(c#反射)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace stri ...

  4. git与github区别与简介

    From: https://blog.csdn.net/skyxmstar/article/details/65631658 git和github是两个完全不同的概念. git 是一个版本管理工具,是 ...

  5. Cache基本原理之:结构

    转载自:https://www.jianshu.com/p/2b51b981fcaf Cache entries 数据在主存和缓存之间以固定大小的”块(block)”为单位传递,也就是每次从main ...

  6. 浅谈Cookie与Session技术

      一.什么是状态管理 将客户端与服务器之间多次交互当做一个整体来看,并且将多次交互所涉及的数据(状态)保存下来. 会话:当用户打开浏览器,访问多个WEB资源,然后关闭浏览器的过程,称之为一个会话,选 ...

  7. gentoo ebuild 私人portage

    最近考虑搞个私人 portage, 用于一些软件的安装和管理. mkdir -p /usr/local/portage/app-misc/hello-world cd $_ cp /usr/porta ...

  8. JDK1.7 的 HashMap

    HashMap是一个用于存储key-value的键值对集合,每个键值对都是一个Entry.这些键值对分散存储在一个数组中,这个数组就是HashMap的主干. HashMap每个初始值都为null. 1 ...

  9. 将LinkedHashMap转换为需要的对象

    项目中,在获取json数据转换为list类型以后,本来以为可以直接使用,结果在使用中报错“java.lang.ClassCastException: java.util.LinkedHashMap c ...

  10. git保存用户名和密码

    git保存用户名和密码 简介:tortoiseGit(乌龟git)图形化了git,我们用起来很方便,但是我们拉取私有项目的时候,每次都要输入用户名和密码很麻烦,这里向大家介绍怎么避免多少输入 试验环境 ...