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. bug6 项目检出JRE问题(Unbound classpath container: 'JRE System Library [JavaSE-1.7]' in project 'idweb')

    项目从SVN检出到工作空间后报了很多错误,其中很明显就是一些jar的问题,没有相关的jar或版本问题,看到最后的错误Unbound classpath Container: 'JRE System L ...

  2. jQuery倒计时代码(超简单)

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

  3. 当input获取倒焦点的时候,获得输入内容

    描述:当用户点击输入框时,获取到他在input里输入的内容 $().keyup(function(){ $(this).val(); }) $(this).val()==this.value; $(t ...

  4. LVS管理工具ipvsadm详解与LVS-NAT模式演示

    ipvsadm命令是工作在用户空间的LVS集群管理工具,原理是调用ipvs的API,在内核中生成ipvs的规则,主要用来管理集群服务和集群服务中的RealServer,使用方法类似iptables命令 ...

  5. linux下怎样查看哪些进程占用swap空间

    for i in `cd /proc;ls |grep "^[0-9]"|awk ' $0 >100'` ;do awk '/Swap:/{a=a+$2}END{print ...

  6. POJ1742Coins(多重背包)

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 32309   Accepted: 10986 Descripti ...

  7. GBDT

    一.决策树分类 决策树分为两大类,分类树和回归树 分类树用于分类标签值,如晴天/阴天/雾/雨.用户性别.网页是否是垃圾页面 回归树用于预测实数值,如明天的温度.用户的年龄 两者的区别: 分类树的结果不 ...

  8. python---生成验证码图片

    工具插件verifycode.py中,记得使用时需要在路由根目录中引入文字资源文件 # coding:utf8 # __author: Administrator # date: // # /usr/ ...

  9. CM记录-Hadoop运维管理

    1.自动化运维--cloudera manager 2.手动运维 a.启动./start.all.sh,停止./stop-all.sh b.hdfs启动/停止:./start-dfs.sh   ./s ...

  10. Http请求中Content-Type讲解以及在Spring MVC中的应用【转】

    完全引用自: http://blog.csdn.net/blueheart20/article/details/45174399#t1   此文讲得很清晰,赞! 引言: 在Http请求中,我们每天都在 ...