119th LeetCode Weekly Contest Subarray Sums Divisible by K
Given an array A
of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K
.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000
已经说过这种题目多是固定格式,或者去Stack Overflow把题目标题复制上去,就有结果给你
class Solution {
public:
int subarraysDivByK(vector<int>& A, int K) {
long long ans = ;
long long k=;
map<long long,long long>a;
a[]=;
int len = A.size();
for(int i=;i<len;i++){
ans = ((ans + A[i])%K+K)%K;
k +=a[ans];
a[ans]++;
}
return k;
}
};
119th LeetCode Weekly Contest Subarray Sums Divisible by K的更多相关文章
- 【LeetCode】974. Subarray Sums Divisible by K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 前缀和求余 日期 题目地址:https:/ ...
- 【leetcode】974. Subarray Sums Divisible by K
题目如下: Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have ...
- 「Leetcode」974. Subarray Sums Divisible by K(Java)
分析 这题场上前缀和都想出来了,然后就没有然后了...哭惹.jpg 前缀和相减能够得到任意一段连续区间的和,然后他们取余\(K\)看余数是否为0就能得到.这是朴素的遍历算法.那么反过来说,如果两个前缀 ...
- [Swift]LeetCode974. 和可被 K 整除的子数组 | Subarray Sums Divisible by K
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...
- 974. Subarray Sums Divisible by K
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...
- Subarray Sums Divisible by K LT974
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...
- LC 974. Subarray Sums Divisible by K
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...
- 119th LeetCode Weekly Contest Largest Perimeter Triangle
Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, ...
- 119th LeetCode Weekly Contest K Closest Points to Origin
We have a list of points on the plane. Find the K closest points to the origin (0, 0). (Here, the d ...
随机推荐
- 新建tomcat的server服务,在左侧项目浏览处,右键空白的地方,选择new,再选择other选项
新建tomcat的server服务,在左侧项目浏览处,右键空白的地方,选择new,再选择other选项. 在弹出的窗口中,下拉滚动条找到Server,并单击next按钮. 在弹出的窗口中,找到第一个A ...
- Nginx+Tomcat集群+session共享
Nginx+Tomcat集群+session共享 1)安装Nginx 2)配置多个Tomcat,在server.xml中修改端口(端口不出现冲突即可) 3)在nginx.conf文件中配置负载均衡池, ...
- SqlServer-geography && Spatial result
说起geography(地理)这个类型,我感觉好陌生,以前真的没有见过,今天在查询某个Address表的时候,却发现了新大陆——Spatial result(空间的结果). (1)表的结构 (2)查询 ...
- 用TCGA数据库分析癌症和癌旁组织的表达差异
上周收到一条求助信息:“如何用TCGA数据库分析LINC00152在卵巢癌与正常组织的的表达差异?” 所以以这个题目为记录分析过程如下: 一.下载数据 a)进入网站https://cancergeno ...
- laravel的mvc
- 【原创】ListView快速滚动至新添加一行(自动滚动)
在C#开发中我们经常要开发一些日志系统,尤其是基于ListView的日志显示系统.但是当日志增多是你是否有一些困扰,就是它为什么不会自动滚动至最后一行. 以下是一小段代码,希望可以帮助你. publi ...
- LIS问题---HDU1025 Constructing Roads In JGShining's Kingdom
发现这个说的比较通俗: 假设存在一个序列d[1..9] = 2 1 5 3 6 4 8 9 7,可以看出来它的LIS长度为5.下面一步一步试着找出它.我们定义一个序列B,然后令 i = 1 to 9 ...
- DB2 函数快速构造测试数据
函数快速构造测试数据 [案例]使用DB2内置函数快速构造测试数据 无论您是在用原型证明某一概念,还是开发一个全新的应用程序,或者只是学习 SQL,您都需要在您的应用程序上运行测试数据.为了有效地测试应 ...
- 解决dragsort鼠标拖动与onclick事件共存
- GeoServer中GeoWebCache(GWC)的使用
本文介绍GeoWebCache的使用方法,包括如何切缓存,访问缓存wms/wmts服务,如何复用栅格缓存等. 文章大部分内容转载自https://www.cnblogs.com/naaoveGIS/p ...