LeetCode Continuous Subarray Sum 题解 同余前缀和 Hash表
题意
给定一个数组和一个整数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表的更多相关文章
- [LeetCode] Continuous Subarray Sum 连续的子数组之和
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- LeetCode Continuous Subarray Sum
原题链接在这里:https://leetcode.com/problems/continuous-subarray-sum/description/ 题目: Given a list of non-n ...
- 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题是存储的当前累加和的 ...
- [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 ...
- [LintCode] Continuous Subarray Sum II
Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...
- LintCode 402: Continuous Subarray Sum
LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...
- Continuous Subarray Sum II(LintCode)
Continuous Subarray Sum II Given an circular integer array (the next element of the last element i ...
- [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 ...
- [LintCode] Continuous Subarray Sum 连续子数组之和
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
随机推荐
- U盘制作macOS Sierra的启动盘
1.macOS Sierra的几种安装方式 *开机时按住command+option+r 进行联网在线安装.PS:在网速好的情况还行,但是如果网络差的时候,它会让你崩溃的. *使用光盘进行安装. *今 ...
- GNU make doc - 3.8
Note that the directory prefix (D), as described in Implicit Rule Search Algorithm, is appended (aft ...
- 不会用数据可视化大屏?一招教你轻松使用数据可视化BI软件创建农业公司运营数据分析大屏
灯果数据可视化BI软件是新一代人工智能数据可视化大屏软件,内置丰富的大屏模板,可视化编辑操作,无需任何经验就可以创建属于你自己的大屏.大家可以在他们的官网下载软件. 本文以农业公司运营数据分析大屏 ...
- linux中的挂载命令
一.查询与自动挂载 查询系统中已经挂载的设备,-l会显示卷标名称 mount [-l] oot@izm5e2q95pbpe1hh0kkwoiz tmp]# mount sysfs on /sys ty ...
- jquery二级导航
效果图 直接放代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- update mysql row (You can't specify target table 'x' for update in FROM clause)
sql语句(update/delete都会出现此问题) update x set available_material_id = null where id not in (select id fro ...
- Linux btrfs文件系统
btrfs,它名字挺多:B-tree fs;Butter fs;Better fs 开源协议是GPL,2007年由Oracle研发 核心特性: 多物理卷支持,btrfs可由多个物理卷组成:支持RAID ...
- mysql 查询出现 "this is incompatible with sql_mode=only_full_group_by"错误解决方案,以及个人rpm方式重装所遇到的问题备份
一.错误说明 这个错误发生在mysql 5.7 版本及以上版本会出现的问题: mysql .7版本默认的sql配置是:sql_mode="ONLY_FULL_GR ...
- (三)LoadRunner术语认识
场景:主要表现为controller中设计与执行测试用例中的用户场景.主要工作有,在controller中选择虚拟用户脚本.设置虚拟用户数量.配置虚拟用户运行时的行为.选择负载发生器.设置执行时间等. ...
- MySQL基础(3) | 函数
MySQL基础(3) | 函数 前言 MySQL只有标量值函数的概念,没有SqlServer那种表值函数. 语法 创建 create function f_add( a int, b int ) re ...