题意

给定一个数组,求连续的子数组的和为k的子数组个数。

思路

连续子数组的和sum[i,j]

sum[i,j]=∑k=ijAk(i&lt;j)sum[i,j]=\sum_{k=i}^jA_k(i&lt;j)sum[i,j]=∑k=ij​Ak​(i<j)

即数组第i个数到第j个数的子数组的和。

sum[i,j]=sum[j]−sum[i−1]sum[i,j]=sum[j]-sum[i-1]sum[i,j]=sum[j]−sum[i−1]其中sum[i]sum[i]sum[i]是前i个数的前缀和。

另子数组的和固定为k,则右边两个数只需要每一个,就可以确定另一个数的取值。

假设我们把前一个sum的下标记为r,后一个记为l.

若我们枚举l,我们有了如下需求:

sum数组中,下标大于l且值为sum[l]+k的个数是多少?

因此我们可以控制l的枚举顺序为从后往前,并且用hash表维护自l往后的sum[i]中的所有<sum值,个数>以应对此查询并且快速更新hash表。


同理,可以从前往后枚举r,用hash表维护下标小于r的所有的<sum值,个数>……

源码

class Solution {
public int subarraySum(int[] nums, int k) {
int len = nums.length;
//key:sum,value:the count of sum[i] where i<r
Map<Integer,Integer> map = new HashMap<Integer, Integer>();
map.put(0,1); // sum[-1]=0
int count = 0;
for (int r = 0,r_sum = 0;r < len; ++r) {
r_sum += nums[r];
int l_sum = r_sum-k;
if (map.containsKey(l_sum))
count += map.get(l_sum);
if (map.containsKey(r_sum))
map.put(r_sum,map.get(r_sum)+1);
else
map.put(r_sum,1);
}
return count;
}
}

结果记录

  • 19ms
  • 82.65%
  • 40.1MB
  • 100.0%

LeetCode subarray-sum-equals-k题解 前缀和+Hash表+枚举——线性做法的更多相关文章

  1. [LeetCode] Subarray Sum Equals K 子数组和为K

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

  2. LeetCode - Subarray sum equals k

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

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

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

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

  6. Subarray Sum & Maximum Size Subarray Sum Equals K && Subarray Sum Equals K

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

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

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

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

随机推荐

  1. 【全集】大数据Linux基础

    课程介绍 本课程是由猎豹移动大数据架构师,根据公司大数据平台的运维情况,精心设计和打磨的大数据必备Linux课程.通过本课程的学习大数据新手能够少走弯路,快速掌握Linux常用命令及Shell编程,为 ...

  2. 文本段落缩进text-indent:2em

    中文文字中的段前习惯空两个文字的空白,这个特殊的样式可以用下面代码来实现: p{text-indent:2em;} <p>1922年的春天,一个想要成名名叫尼克卡拉威(托比?马奎尔Tobe ...

  3. JMeter函数助手中Random函数详解

    1.__Random函数的生成 The minimum value allowed for a range of values:一个范围内允许的最小值 The maximum value allowe ...

  4. linux中权限管理命令(chmod/chown/chgrp/unmask)

    目录 chmod chown chgrp umask chmod 解释 命令名称:chmod 命令英文原意:change the permissions mode of a file 命令所在路径:/ ...

  5. linux中find文件搜索命令

    find 解释 命令名称:find 命令所在路径:/bin/find 执行权限:所有用户 功能描述:文件搜索 语法 find [搜索范围] [匹配条件] 匹配条件: -name 文件名(区分大小写) ...

  6. Java @Deprecated Annotation(注解)

    在本部分的快速指南中,我们将会查看 Java 的 deprecated API 和如何在程序中使用 @Deprecated 注解. @Deprecated Annotation(注解) 作为程序的进化 ...

  7. PPT导出为图片

    使用Aspose组件导出 Aspose有Aspose.Slides.dll,可以无需安装office,进行读写PPT文件. Aspose可能通过Aspose.Slides.NET安装 简单的导出图片d ...

  8. 11种常用css样式学习大结局滚动条与显示隐藏

    滚动条展示 overflow-x: hidden;/*是否对内容的左/右边缘进行裁剪*/overflow-y: hidden;/*是否对内容的上/下边缘进行裁剪*/overflow:scroll;/* ...

  9. TP6文档-邓士鹏

    2019年5月11日 - 教程为您提供<ThinkPHP6.0极速入门(视频教程)>之 TP6的目录结构 章节的在线实战教程供您学习,你可以进行笔记.提问.讨论和资料下载 https:// ...

  10. npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules

    Mac 权限不够  前面加sudo   然后输入密码