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:

  1. Input:nums = [1,1,1], k = 2
  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].
  1. class Solution(object):
  2. def subarraySum(self, nums, k):
  3. """
  4. :type nums: List[int]
  5. :type k: int
  6. :rtype: int
  7. """
  8. sums = {0:1}
  9. s = 0
  10. cnt = 0
  11. for i in nums:
  12. s += i
  13. if sums.get(s-k) != None:
  14. cnt += sums[s-k]
  15. if sums.get(s) == None:
  16. sums[s] = 1
  17. else:
  18. sums[s] += 1
  19. 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. 由asp的一个错误,看语言的不同:asp & java

    今天查看网页源代码,无意发现源代码尾部抛出asp的错误信息,但没有显示在网页上. 如果没查看源代码,还没发现asp运行代码出错了. 大致情况是这样,在asp中,有一个变量来表示用户当前使用的容量,注意 ...

  2. configParse模块(二十七)

    configparser用于处理特定格式的文件,其本质上是利用open来操作文件. # 注释1 ; 注释2 [section1] # 节点 k1 = v1 # 值 k2:v2 # 值 [section ...

  3. kindeditor上传图片的大小在哪控制

    请修改修改了multiimage.js 的imageSizeLimit = K.undef(self.imageSizeLimit, '3MB') 大小设置级可以

  4. Apache Solr 初级教程(介绍、安装部署、Java接口、中文分词)

    Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...

  5. php脚本#!/usr/bin/env php 写法

    脚本语言的第一行,目的就是指出,你想要你的这个文件中的代码用什么可执行程序去运行它. 比如php脚本的第一行可以写成如下几种格式 #!/usr/bin/php #!/usr/bin/env php # ...

  6. linux的一个find命令配合rm删除某天前的文件

    语句写法: find 对应目录 -mtime +天数 -name "文件名" -exec rm -rf {} \; 例1: 将/usr/local/backups目录下所有10天前 ...

  7. sql server复制数据到excel格式变成字符串

    sql server复制数据到excel格式变成字符串,结果数据都保存在第一个格子里面. 我点击连同标题一起复制,然后粘贴到excel,结果是这样子的.... 这不是我想要的结果,在网上查询了好多,结 ...

  8. POJ - 1019 Number Sequence (思维)

    https://vjudge.net/problem/POJ-1019 题意 给一串1 12 123 1234 12345 123456 1234567 12345678 123456789 1234 ...

  9. Host is not allowed to connect to this MySQL server解决方法

    在装有MySQL的机器上登录MySQL mysql -u root -p密码 执行use mysql; 执行update user set host = '%' where user = 'root' ...

  10. MySql Workbench导出ER图并存为PDF文件

    一.登陆数据库 二.点击Database  => Reverse Engineer 三.填入登陆信息后next => next,选择要生成ER模型的数据库 四.点击next => n ...