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. R语言 画图roc

    这才是我要的滑板鞋~~~~~ #glm模型glm.model=train(y~.,data=data_train, method="glm", metric="ROC&q ...

  2. struts下载

    struts下载地址:http://struts.apache.org/download.cgi

  3. authentication failed for xxx错误

    现象: 公司windows定期修改过密码后 一直报错.push的时候显示“Authentication Failed for http://x.x.x.x/x/git” 猜想: 发现可能是账号问题. ...

  4. Git基础考试题

    Git: 1.在windows上搭建git环境,成功后查看版本号 Linux安装 sudo apt-get install git -y yum install git -y windows直接安装e ...

  5. Vue加载json文件

    一.在build/dev-server.js文件里 var app = express() 这句代码后面添加如下(旧版): var appData = require('../address.json ...

  6. div 只显示两行超出部分隐藏

    ; -webkit-box-orient: vertical;line-height: 26px } <td rowspan="2" colspan="2" ...

  7. Linux之常用命令【service】

    补充说明 service命令 是Redhat Linux兼容的发行版中用来控制系统服务的实用工具,它以启动.停止.重新启动和关闭系统服务,还可以显示所有系统服务的当前状态. 语法 service(选项 ...

  8. Linux之目录结构解析

    /    /bin 存放[二进制可执行命令]目录,与usr/bin相比,它是系统性的.主要放置一些系统的必备执行档.例如:cat.cp.chmod df.dmesg.gzip.kill.ls. mkd ...

  9. 有关ACM学习的博客链接

    大综合: 杭电OJ水题大全题解:http://blog.csdn.net/ysc504?viewmode=contents 14级浙江财经大学大佬:http://blog.csdn.net/jtjy5 ...

  10. Java导出txt模板——(一)

    导出txt文件时候\r\n才能换行 java代码 package DRDCWordTemplates; import java.io.BufferedWriter; import java.io.Fi ...