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].
 class Solution(object):
def subarraySum(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
sums = {0:1}
s = 0
cnt = 0
for i in nums:
s += i
if sums.get(s-k) != None:
cnt += sums[s-k]
if sums.get(s) == None:
sums[s] = 1
else:
sums[s] += 1
return cnt

560. Subarray Sum Equals 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 (子数组之和等于K)

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

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

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

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

  7. 【leetcode】560. Subarray Sum Equals K

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

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

  9. Subarray Sum & Maximum Size Subarray Sum Equals K

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

随机推荐

  1. 【POJ3694】Network

    题目大意:给定一个 N 个点,M 条边的无向图,支持 Q 次操作,每次可以向该无向图中加入一条边,并需要回答当前无向图中桥的个数. 题解:(暴力:Q 次 Tarjan) 先进行一次 Tarjan 求出 ...

  2. [luogu2114][起床困难综合症]

    luogu2114 思路 因为位运算对于每一位是独立的,所以对每一位都对这n个数进行操作,然后观察最后得出的是1还是0.并且保证每一位拼起来之后要比m小. 代码 #include<cstdio& ...

  3. Chrome 下,重复使用 XMLHttpRequest进行Post数据时,遇到一个奇怪的问题

    var http_request; //在外面申明对象,主要为了在updatePage中使用     //无刷新更新内容 function post(url,parameter) {        i ...

  4. 安装 scrapy 报错 error: Microsoft Visual C++ 14.0 is required

    问题描述 使用 pip install scrapy 安装 scrapy 时出现以下错误: error: Microsoft Visual C++ 14.0 is required 错误提示中给出了一 ...

  5. Missing artifact com.github.pagehelper:pagehelper:jar:3.4.2-fix的解决方法(最简单的方法)

    在网上看的淘淘商城的项目,自己在配置maven项目的时候遇见了这个异常,按照网上教程试了试,一重启各种异常. 后来直接,就更改了自己的maven仓库就ok了. 解决方法: 对比一下,你就能够发现问题, ...

  6. Linux命令之cd

    cd命令 用处:跳转目录 用法:输入cd加上你想跳转的目录,这里分几种情况 示例: 一.进入当前目录的子目录 我现在的目录是 /home/jim,如图 这个目录下面有好多文件夹是吧,现在我想进入到其中 ...

  7. javascript 拖拽事件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. ruby where用法

    用法1 Subject.where(").order("name") 用法2 与find方法不同的是,where方法返回的结果不是数组而是ActiveRelation,这 ...

  9. JQuery弹出层,点击按钮后弹出遮罩层,有关闭按钮【转】

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

  10. 如何使用less(变量,混合,匹配,运算,嵌套...)

    如何使用less及一些常用的(变量,混合,匹配,运算,嵌套...) less的介绍及编译工具 什么是less 1.LESSCSS是一种动态样式语言,属于CSS预处理语言的一种,它使用类似CSS的语法, ...