作者: 负雪明烛
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. Linux之文件读取查看之cat、head、tail、tac、rev、more、less

    Linux文件查看的命令有很多,如cat.head.tail.tac.rev.more.less等 1. cat之查看文件内容 NAME cat - 连接文件并在标准输出上打印(concatenate ...

  2. MongoDB的搭建、参数

    Mongodb官网:https://www.mongodb.com/ mkdir -r  /data/db touch  /data/log tar -zxvf mongodb-linux-x86_6 ...

  3. 【模板】网络最大流(EK、Dinic、ISAP)(网络流)/洛谷P3376

    题目链接 https://www.luogu.com.cn/problem/P3376 题目大意 输入格式 第一行包含四个正整数 \(n,m,s,t\),分别表示点的个数.有向边的个数.源点序号.汇点 ...

  4. C#筛选项联动,联动筛选

    protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { FillDep(); } // FillDG(); ...

  5. android知识点duplicateParentState

    android知识点duplicateParentState 今天要做一个效果,组件RelativeLayout上有两个TextView,这两个TextView具有不同的颜色值,现在要的效果是,当Re ...

  6. 【编程思想】【设计模式】【其他模式】hsm

    Python版 https://github.com/faif/python-patterns/blob/master/other/hsm/hsm.py """ Impl ...

  7. Linux基础命令---nfsstat显示nfs信息

    nfsstat nfsstat指令用来显示nfs客户端和服务器的活动信息. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora. 1.语法       nfsstat  ...

  8. treeTable实现排序

    /* * * TreeTable 0.1 - Client-side TreeTable Viewer! * @requires jQuery v1.3 * * Dual licensed under ...

  9. SVN的基本介绍\服务器配置

    ### 1. 工作场景 1. 进入公司需要做的关于开发的第一件事, 就是向项目经理索要SVN服务器地址+用户名+密码### 2. 角色解释> 服务器: 用于存放所有版本的代码,供客户端上传下载更 ...

  10. 商城项目的购物车模块的实现------通过session实现

    1.新建购物车的实体类Cart public class Cart implements java.io.Serializable{ private Shangpin shangpin;//存放商品实 ...