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 ...
随机推荐
- realsense and Mask_RCNN
###################librealsense and Mask_RCNN cd RealSennse/librealsense2018091501/librealsense/wrap ...
- opennebule 创建cdrom数据发送
{","csrftoken":"b9b5026f1a92180b789971ed8e21d28b"}
- css 样式文字溢出显示省略号
在table中使用溢出样式,table样式要设置为”table-layout: fixed“,即<table style="table-layout: fixed;"> ...
- Ubuntu 查看软件版本
Boost dpkg -S /usr/include/boost/version.hpp OpenCV pkg-config --modversion opencv
- 7.内网渗透之windows认证机制
文章参考自三好学生域渗透系列文章 看了内网渗透第五篇文章,发现如果想要真正了解PTT,PTH攻击流程,还需要了解windows的认证机制,包括域内的kerberos协议. windows认证机制 在域 ...
- Python基础入门-函数实战登录功能
''' 函数实战: .加法计算器 .过滤器 .登录功能实战 ''' def add(a,b): return a+b def login_order(): return 'asdfasdfdasfad ...
- easyui 列表 条件检索
onclick="search()" 不要使用search命名检索方法,冲突,无法调用. 通用检索function function searchData() { var objs ...
- (转)每位设计师都应该拥有的50个CSS代码片段
原文地址:http://www.cnblogs.com/fengyuqing/archive/2013/06/15/css_50.html 面对每年如此多的 新趋势 ,保持行业的领先是个很困难问题. ...
- 编写高质量代码改善C#程序的157个建议——建议41:实现标准的事件模型
建议41:实现标准的事件模型 上一建议中,我们实现了一个带事件通知的文件传输类FileUploader.虽然已经满足需求,但却不符合C#的编码规范,查看EventHandler的原型声明: publi ...
- 牌型总数——第六届蓝桥杯C语言B组(省赛)第七题
原创 牌型种数 小明被劫持到X赌城,被迫与其他3人玩牌.一副扑克牌(去掉大小王牌,共52张),均匀发给4个人,每个人13张.这时,小明脑子里突然冒出一个问题:如果不考虑花色,只考虑点数,也不考虑自己得 ...