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 ...
随机推荐
- expect知识梳理
1 expect expect软件用于实现非交互式操作,实际应用中常用于批量部署,可以帮助运维人员管理成千上万台服务器. expect实现非交互式操作主要是在程序发出交互式询问时,按条件传递程序所需的 ...
- NIO-WindowsSelectorImpl源码分析
目录 NIO-WindowsSelectorImpl源码分析 目录 前言 初始化WindowsSelectorProvider 创建WindowsSelectorImpl WindowsSelecto ...
- R语言入门:向量索引
这节的内容是建立在之前我们对R语言最基本向量赋值的基础之上的,笔者本人学完R当中向量的索引感觉异常舒适,因为这个比Python的索引爽多了,是什么值开始索引就从哪里开始索引,到哪里结束就在哪里结束,而 ...
- Maven 父子工程的一些细节
Project,项目,也叫做工程. 父子工程中,子模块会自动继承父工程的资源.依赖,但子模块之间是独立的,不能直接访问彼此中的资源.类. 就是说我们可以把多个子模块都要用的资源.依赖提出来,放到父工程 ...
- 【转】Makefile步步为营
Makefile步步为营 本目录主要包含Makefile一步步递进学习的示例代码 makefile代码实例:https://www.lanzous.com/i9m9npi step0:Makefile ...
- 【React Native】在网页中打开Android应用程序
React Native官方提供Linking库用于调起其他app或者本机应用.Linking的主要属性和方法有: 属性与方法 canOpenURL(url); 判断设备上是否有已经安装相应应用或可以 ...
- Dijkstra算法 1
// Dijkstra算法,适用于没有负边的情况 // 注意:是没有负边,不是没有负环 // 在这一条件下,可以将算法进行优化 // 从O(v*E)的复杂度,到O(V^2)或者是O(E*log(V)) ...
- Github无法访问的解决办法
#github 192.30.253.113 github.com 192.30.253.113 github.com 192.30.253.118 gist.github.com 192.30.25 ...
- 常用 PostgreSQL 脚本
数据定义 数据库 -- 创建数据库 -- https://www.postgresql.org/docs/current/static/multibyte.html -- database_name, ...
- FIB表与RIB表的区别与联系
RIB (route information base) 和 FIB (forwarding information base),又称Ip路由表 和 CEF表,它们之间的关系可以用下面这张图片来高度概 ...