作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/subarray-sum-equals-k/description/

题目描述

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:

  1. The length of the array is in range [1, 20,000].
  2. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].

题目大意

求数组中有多少个连续子数组的和正好等于k。

解题方法

看了数组的长度,明显O(n^2)的时间复杂度会超时。这个时间复杂度一般只能用O(N)的解法了。

使用一个字典保存数组某个位置之前的数组和,然后遍历数组求和,这样当我们求到一个位置的和的时候,向前找sum-k是否在数组中,如果在的话,更新结果为之前的结果+(sum-k出现的次数)。同时,当前这个sum出现的次数就多了一次。

这个字典的意义是什么呢?其意义就是我们在到达i位置的时候,前i项的和出现的次数的统计。我们想找的是在i位置向前的连续区间中,有多少个位置的和是k。有了这个统计,我们就不用向前一一遍历找sum - k在哪些位置出现了,而是直接得出了前面有多少个区间。所以,在每个位置我们都得到了以这个位置为结尾的并且和等于k的区间的个数,所以总和就是结果。

这个题的解法不难想出来,因为如果要降低时间复杂度,应该能想到增加空间复杂度,那么要么使用数组,要么就是用字典之类的,保留之前的结果。

时间复杂度是O(N),空间复杂度是O(N).

代码如下:

class Solution(object):
def subarraySum(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
n = len(nums)
d = collections.defaultdict(int)
d[0] = 1
sum = 0
res = 0
for i in range(n):
sum += nums[i]
if sum - k in d:
res += d[sum - k]
d[sum] += 1
return res

参考资料:

https://www.youtube.com/watch?v=mKXIH9GnhgU

日期

2018 年 9 月 19 日 —— 梦见李彦宏和我聊微信,他感谢我给一个短视频App做了分享功能……
2019 年 1 月 4 日 —— 这周就过去了

【LeetCode】560. Subarray Sum Equals K 解题报告(Python)的更多相关文章

  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 和为K的子数组

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

  5. 【LeetCode】325. Maximum Size Subarray Sum Equals k 解题报告 (C++)

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

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

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

  8. 560. Subarray Sum Equals K

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

  9. 【leetcode】560. Subarray Sum Equals K

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

随机推荐

  1. C语言 指针数组指针

    指向指针数组的指针. 1 #include <stdio.h> 2 3 int main(void) 4 { 5 char *pa[4]={"aaaaa"," ...

  2. 35-Remove Element

    Remove Element My Submissions QuestionEditorial Solution Total Accepted: 115367 Total Submissions: 3 ...

  3. CentOS安装配置Hadoop 1.2.1(伪分布模式)

    CentOS安装配置Hadoop1.2.1 1.下载安装文件 下载2个安装文件 JAVA环境:jdk-6u21-linux-i586.bin Hadoop环境:hadoop-1.2.1.tar.gz ...

  4. 《Redis设计与实现》知识点目录

    Redis设计与实现 第一部分 数据结构与对象 第二章 简单动态字符串 p8 简单动态字符串SDS 2.1 SDS的定义 p9 每个sds.h/sdshdr结构表示一个SDS值 2.2 SDS与C字符 ...

  5. 阿里云NAS性能测试

    测试方法:根据阿里云NAS官方文档进行测试 测试对象:性能型NAS,总容量1PB,已使用27.49GB(计算吞吐量时按30GB计算) 随机读IOPS测试 测试命令 fio -numjobs=1 -io ...

  6. Vue相关,diff算法。

    1. 当数据发生变化时,vue是怎么更新节点的? 要知道渲染真实DOM的开销是很大的,比如有时候我们修改了某个数据,如果直接渲染到真实dom上会引起整个dom树的重绘和重排,有没有可能我们只更新我们修 ...

  7. c学习 - 第五章:选择结构程序设计

    5.2 关系运算符与逻辑运算符 !(非) ^ 高 算术运算符 | 关系运算符 | &&和 || | 赋值运算符 | 低

  8. 【Spring Framework】Spring入门教程(六)Spring AOP使用

    Spring的AOP 动态代理模式的缺陷是: 实现类必须要实现接口 -JDK动态代理 无法通过规则制定拦截无需功能增强的方法. Spring-AOP主要弥补了第二个不足,通过规则设置来拦截方法,并对方 ...

  9. solr8.2

    https://www.cnblogs.com/carlosouyang/p/11352779.html

  10. 【力扣】82. 删除排序链表中的重复元素 II

    存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 没有重复出现 的数字. 返回同样按升序排列的结果链表. 示例 1: 输入:hea ...