题意

给定一个数组和一个整数k,返回是否存在一个长度至少为2的连续子数组的和为k的倍数。

思路

和上一篇博客的思路基本一致。

LeetCode subarray-sum-equals-k题解

所不同的是,子数组至少长度为2。因此需要一个缓冲区,延缓往Hash表中加数的操作。

另外,因为是和变成是k的倍数。利用同余的知识易得我们维护的前缀和是群ZkZ_kZk​中的和。


ps.这里我犯了一个错误,没有考虑k取0的情况,当值RE一次,然后简单的改成返回true再次WA一次。

特殊情况k=0

不在取模,不考虑同余了。注意到0的倍数的集合中只有0一个元素,因此直接变成和为k=0的情况即可。

Source Code 1

考虑到子数组的最小大小如果变化的通用性,我用了一个队列做缓冲。

现在写博客的时候突然想到其实可以用两个sum,同步移动,分别往后算就好了。一个负责枚举的右边的sum,一个负责要更新hash表的sum.也可以实现通用性。

结果:

  • 8 ms 49.62%
  • 40.9 MB 100%

    嫌49.62%太低,我就改了一下延迟更新hash表的方式。
class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
final int minSubArrLen = 2;
int len = nums.length;
if (len < minSubArrLen)
return false;
Queue<Integer> q = new LinkedList<Integer>();
int sum = 0;
q.offer(sum); // sum[-1]
int i;
for (i =0; i < minSubArrLen-1; ++i) {
sum = f((sum+nums[i]),k);
q.offer(sum); // sum[i]
}
//key:sum(mod k),value:the count of sum[i] where i<r
Set<Integer> l = new HashSet<Integer>();
int tmp;
for (;i < len; ++i) {
tmp = q.poll();
l.add(tmp);
sum = f((sum+nums[i]),k);
q.offer(sum);
if (l.contains(sum))
return true;
}
return false;
} int f(int a,int b) {
return b == 0 ? a : a%b;
}
}

Source Code 2

结果

  • Runtime: 6 ms, faster than 94.01%
  • Memory Usage: 41.3 MB, less than 100.00%
class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
int len = nums.length;
if (len < 2)
return false;
int sum = 0;
//key:sum(mod k),value:the count of sum[i] where i<r
Set<Integer> l = new HashSet<Integer>();
l.add(0); // sum[-1]
sum = (k != 0) ? (nums[0])%k : nums[0]; //sum[0]
int t = sum;
for (int i = 1;i < len; ++i) {
sum = (k != 0) ? (sum+nums[i])%k : sum+nums[i];
if (l.contains(sum))
return true;
l.add(t);
t = sum;
}
return false;
}
}

LeetCode Continuous Subarray Sum 题解 同余前缀和 Hash表的更多相关文章

  1. [LeetCode] Continuous Subarray Sum 连续的子数组之和

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

  2. LeetCode Continuous Subarray Sum

    原题链接在这里:https://leetcode.com/problems/continuous-subarray-sum/description/ 题目: Given a list of non-n ...

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

  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. [LintCode] Continuous Subarray Sum II

    Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...

  6. LintCode 402: Continuous Subarray Sum

    LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...

  7. Continuous Subarray Sum II(LintCode)

    Continuous Subarray Sum II   Given an circular integer array (the next element of the last element i ...

  8. [leetcode]523. Continuous Subarray Sum连续子数组和(为K的倍数)

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

  9. [LintCode] Continuous Subarray Sum 连续子数组之和

    Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...

随机推荐

  1. linux中查看nginx、apache、php、mysql配置文件路径

    linux高效.稳定,但是也带来维护上的一些问题.配置文件究竟在哪里????? 如何在linux中查看nginx.apache.php.mysql配置文件路径了,如果你接收一个别人配置过的环境,但没留 ...

  2. 手把手带你阅读Mybatis源码(三)缓存篇

    前言 大家好,这一篇文章是MyBatis系列的最后一篇文章,前面两篇文章:手把手带你阅读Mybatis源码(一)构造篇 和 手把手带你阅读Mybatis源码(二)执行篇,主要说明了MyBatis是如何 ...

  3. toj 3086 Passage (不错)

    Passage 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交: 40 测试通过: 20 描述 Bill is a millionaire. But ...

  4. leetcode—js—Add Two Numbers

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  5. C# 如何实现完整的INI文件读写类

    作者: 魔法软糖 日期: 2020-02-27 引言 ************************************* .ini 文件是Initialization File的缩写,即配置文 ...

  6. CHECK INDEX OF TABLE

    SELECT INDEX_NAME,COLUMN_NAME FROM ALL_IND_COLUMNS WHERE table_name = '';

  7. 修改计算机名并更新sqlserver中存储的服务器名称

    1.  查看计算机名use master    go     select @@servername   select serverproperty('servername') 2.同步更新SQLse ...

  8. CentOS7.6安装MySQL8.0(图文详细篇)

    目录 一.安装前准备 二.安装MySQL 三.设置远程登录 四.安装问题解决 五.设置MySQL开机自启 一.安装前准备 1.在官网下载MySQL安装包(注意下载的安装包类型)  2.查看是否安装ma ...

  9. 如何获取 iOS APP 的 scheme URL ?

    获取IPA文件 拷贝到桌面上 后缀名由 .ipa 改为 .zip 解压之后进入,进入名为Payload的目录 右键点击里面的跟App同名的文件,选择'显示包内容' 用文本编辑器打开当前文件夹下的inf ...

  10. Excel——排序筛选

    1,自定义排序:多个关键字,从右向左一一排序 * 按颜色排序 * 按自定义序列排序 *两列中,列一个中间数,升序 * 打印标题行 * 选中,定位条件(可见),选择 * 数值筛选(大于等于),文本筛选( ...