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. python(六)——基本数据类型介绍

    1.数字整形 python3不管数字有多大都是int型,没有long类型 1>字符串转换为数字 s1 = " print(type(s1),s1) b = int(s1)#不加base ...

  2. xor定理证明

    xor 证明: 0 xor 0=0 0 xor 1=1 1 xor 0=1 1 xor 1=0 0 xor 其它数,数值不会改变1 xor 其它数,数值会反转 所以x个数0和y个数1进行xor运算(0 ...

  3. Druid数据源对数据库访问密码加密好麻烦

    开发中,druid数据源对数据库密码进行了加密,每次切换数据库或者修改密码后,感觉很麻烦. 解决办法: 1.用工具类中的Java代码进行加解密. 需要用到com.alibaba.druid.filte ...

  4. (大数 求余) Large Division Light OJ 1214

    Large Division Given two integers, a and b, you should check whether a is divisible by b or not. We ...

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

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

  6. 学习windows编程 day5 之按键消息

    case WM_KEYDOWN://带sys的按键消息大多是系统需要自己处理的,我们一般不需要,默认处理 //wParam 指定按键的虚拟键代码 //lParam 指定技术,扫描码,闲钱状态,转换状态 ...

  7. 学习windows编程 day4 之 多边矩形填充

    #include <windows.h> #include <math.h> LRESULT CALLBACK WndProc(HWND hwnd, UINT message, ...

  8. javascript 值类型和引用类型

    值类型 1. 值类型:string/number/boolean/undefined: 2. 存储:值类型的数据,存储的是数据本身的变量: 3. 赋值:直接将存储的数据复制一份进行赋值,两份数据在内存 ...

  9. js正则表达式【原】

    js正则表达式 http://www.w3school.com.cn/js/js_obj_regexp.asp js常用正则表达式 我的自测样例 <HTML> <HEAD> & ...

  10. Calendar 日历类的时间操作

    我们经常会涉及到对时间的处理,例如登陆网站,我们会看到网站首页显示XXX,欢迎您!今天是XXXX年....某些网站会记录下用户登陆的时间,比如银行的一些网站,对于这些经常需要处理的问题,Java中提供 ...