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 ...
随机推荐
- SpringBoot25 gradle安装、利用gradle创建SrpingBoot项目
1 gradle安装 技巧01:gradle依赖JDK或者JRE,而且版本至少时1.7 1.1 下载安装包 到gradle官网下载安装包[PS: 也可以利用命令的方式安装,本案例是利用安装包的方式] ...
- iBase4j前端01_bootstrap-suggest json-server模拟后台数据、bootstrap-suggest环境搭建、开启bootstrap-suggest的post和put请求
1 准备 1.1 模拟的json数据 { "info": [ { "message": "信息", "value": [ ...
- 535. Encode and Decode TinyURL 长短URL
[抄题]: TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problem ...
- struts2 框架的基本使用
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...
- launchpad, jira, github
一.简介 http://segmentfault.com/q/1010000000165115
- ESP8266文档阅读2A-SDK-Espressif IoT SDK 使用手册v1.0.1.pdf
2A-SDK-Espressif IoT SDK 使用手册v1.0.1.pdf 1.前言 本⽂文主要介绍基于ESP8266物联⺴⽹网模块的SDK相关使⽤用⽅方法,包括开发⼯工具使⽤用以及SDK软件包架 ...
- POJ1125 Stockbroker Grapevine(spfa枚举)
Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a me ...
- CString::MakeLower Crash
记录一下使用CString::MakeLower可能导致的crash的一个问题: 问题重现: int _tmain(int argc, _TCHAR* argv[]){ std::string sT ...
- .Net Core 项目部署IIS简单步骤
1.新建一个解决方案: 我习惯会把运行文件移至一级目录 然后清除CoreTest 文件夹里面的文件 2.在解决方案中新建一个项目 点击确认有,这里有几种选择类型,我一般选择空类型(这里需要注意一下,空 ...
- vs2015 使用 Eazfuscator.NET 3.3
出现问题: Unable to cast object System.Xml.XmlComment’ to type ‘System.Xml.XmlElement’ 解决办法: 打开 *.csproj ...