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

这个题目思路是利用cumulative sum, 前面数字之和去计算. 参考Leetcode 原题Solution.

建一个diction, 记录 sum 出现过的次数, 然后每次用sum - target, 如果存在在dction里面, count += d[sum-target], 最后返回count. 如果直到这个思路之后其实不难, 否则还蛮难想的.

class Solution:
def subarraySum(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
""" d, count,s = {0:1}, 0,0 # 最开始有0:1 是因为"" 的sum是0, 所以初始为1
for num in nums:
s += num
if s - k in d:
count += d[s-k]
if s not in d:
d[s] = 1
else:
d[s] += 1 return count

[LeetCode] 560. Subarray Sum Equals K_Medium的更多相关文章

  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】560. Subarray Sum Equals K 解题报告(Python)

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

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

  7. 560. Subarray Sum Equals K

    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. Esper学习之一:Esper介绍

    CEP即Complex Event Process,中文意思就是“复杂事件处理”.听起来好像很复杂,实际上就是基于事件流进行数据处理,把要分析的数据抽象成事件,然后将数据发送到CEP引擎,引擎就会根据 ...

  2. 小程序判断是否授权源码 auth.js

    一.auth.js const configGlobal = require('../config/config_global.js'); var util = require('function.j ...

  3. router之switch

    比较路由中有无switch的区别: 代码一: <Router history={history}> <Route exact path="/" component ...

  4. git查看指令

    打开git bash 1,查看自己之前是否生成过ssh密钥 $ ls .ssh 如果存在这个id_rsa.pub这个文件的话表示已经生成了 2,查看用户名和邮箱 $ git config --glob ...

  5. HDU 2199 Can you solve this equation(二分答案)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  6. jquery.fly.min.js 拋物插件

    插件官方: https://github.com/amibug/fly, 官方例子: http://codepen.io/hzxs1990225/full/ogLaVp 首先加载jQuery.js和j ...

  7. nagios监控报错 It appears as though you do not have permission to view...

    今天在安装完nagios后,通过nagios网页界面点击主机.服务.问题页面时.均报错,报错的内容都差不多.如点击服务,报错: It appears as though you do not have ...

  8. [工具] Snipaste

    https://zh.snipaste.com/ Snipaste 是一个简单但强大的截图工具,也可以让你将截图贴回到屏幕上! 下载并打开 Snipaste,按下 F1 来开始截图, 选择“复制到剪贴 ...

  9. mac操作

    资料搜集: mac终端 常用命令操作 mac osx常用快捷键一览 mac chrome快捷键

  10. 解决pycharm安装包过程出现的问题:module 'pip' has no attribute 'main'

    解决pycharm安装包过程出现的问题:module 'pip' has no attribute 'main' 问题 更新pip之后,Pycharm安装package出现如下报错:module 'p ...