题目如下:解题思路:本题的关键在于题目限定了是连续的数组,我们用一个dp数组保存第i位到数组末位的和。例如nums = [1,1,1],那么dp = [3,2,1], dp[i]表示nums[i]+nums[i+1] +...+nums[len(nums)-1],有了这一个dp数组后,我们很容易就可以得到递推表达式 sum(i,j) = dp[i] - dp[j+1]。最后,顺序遍历dp数组,对于任意的dp[i],只要找到对应的dp[k-i]就可以了。

代码如下:

class Solution(object):
def subarraySum(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
dp = [0 for x in nums]
dp[-1] = nums[-1]
dic = {}
dic[dp[-1]] = 1
for i in xrange(-2,-len(nums)-1,-1):
dp[i] = dp[i+1] + nums[i]
if dic.has_key(dp[i]):
dic[dp[i]] += 1
else:
dic[dp[i]] = 1
res = 0
for i,v in enumerate(dp):
if v == k:
res += 1
dic[v] -= 1
if dic.has_key(v-k):
res += dic[v-k] return res

【leetcode】560. Subarray Sum Equals K的更多相关文章

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

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

  2. 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题是存储的当前累加和的 ...

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

  6. [LeetCode] 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 ...

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

  8. 560. Subarray Sum Equals K

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

  9. LeetCode Maximum Size Subarray Sum Equals k

    原题链接在这里:https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/ 题目: Given an array nums an ...

随机推荐

  1. Delphi DbgridEh实现鼠标拖动选中列,并使复选框选中

    1.先设置表格列的属性 procedure TForm_TaskToDW.InitGrid;var  MyCol: TColumnEh;begin  with DBGridEh_Task do  be ...

  2. wpf slider刻度

    TickFrequency:刻度之间的间隔   IsSnapToTickEnabled:是否对齐到刻度   TickPlacement:刻度位置

  3. CDS究竟是个什么鬼?它直接导致了次贷危机?

    周五,中国银行间市场交易商协会就确认了这一消息,信用违约互换(CDS)和信用联结票据(CLN)业务指引在今日正式发布实行. 当然,这则消息在中国普通投资者当中还没引起足够关注,但是在很多人看来CDS这 ...

  4. c++ 调用 sqlcipher

    #include <iostream> #include <string.h> #include "sqlite3.h" using namespace s ...

  5. 一图看懂python对excel的读写

  6. sentos7忘记root密码,重置密码

    一.两种模式:单用户模式和救援模式 下面示例救援模式 1.重启linux系统主机并出现引导界面,按e键进入内核编辑界面: 2.在linux16参数那一行的最后面追加“rd.break”参数,记住要空开 ...

  7. Monkey学习笔记(一)

    (一)adb相关命令语句: 1. 查看连接设备信息:adb devices 2.安装app到手机上:adb install [-r]  [apk文件存在地址].apk 3.将文件放入设备/模拟器:ad ...

  8. HDU 1231 题解

    题面: 最大连续子序列 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem ...

  9. Simpsons’ Hidden Talents

    Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had. Marg ...

  10. 通过编写串口助手工具学习MFC过程——(三)Unicode字符集的宽字符和多字节字符转换

    通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...