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. 1 <= A.length <= 30000
  2. -10000 <= A[i] <= 10000
  3. 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的更多相关文章

  1. 【LeetCode】974. Subarray Sums Divisible by K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 前缀和求余 日期 题目地址:https:/ ...

  2. 【leetcode】974. Subarray Sums Divisible by K

    题目如下: Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have ...

  3. 「Leetcode」974. Subarray Sums Divisible by K(Java)

    分析 这题场上前缀和都想出来了,然后就没有然后了...哭惹.jpg 前缀和相减能够得到任意一段连续区间的和,然后他们取余\(K\)看余数是否为0就能得到.这是朴素的遍历算法.那么反过来说,如果两个前缀 ...

  4. [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 ...

  5. 974. Subarray Sums Divisible by K

    Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...

  6. Subarray Sums Divisible by K LT974

    Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...

  7. 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 ...

  8. 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, ...

  9. 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 ...

随机推荐

  1. 13-STL-二分查找

    STL中提供-二分查找算法(binary_search lower_bound upper_bound equal_range   STL包含四种不同的二分查找算法,binary_search    ...

  2. c语言实践 数字特征值

    这题的要求是这样的: 这题我没做出来,我大概思路是这样的,根据输入的数字,把这个数字的每一位都分离出来,然后判断奇数还是偶数,再判断序是奇数还是偶数,最后两个奇偶性比较,输出1还是0,这个输出的1和0 ...

  3. Apache apxs命令

    一.简介 apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象,使之可以用由mod_so提供的LoadModule指令在运行时加载 ...

  4. dataview 组件使用示例

    来自<sencha touch 权威指南> ------------------------------- 例子1——app.js代码如下: Ext.require(['Ext.data. ...

  5. zigbee之MAC地址发送

    TI cc2530在出厂时候每一个芯片都固化了一个唯一的8个字节的地址,MAC或者IEEE地址. 协调器模块的MAC地址为:0x00124B000716550F(注意自己的是多少!!) 终端的MAC地 ...

  6. 2.python IP/DNS地址处理之IPy/Dnspython模块

     1.IPy模块 在IP地址规划中,涉及到计算大量的IP地址,包括网段.网络掩码.广播地址.子网数.IP类型等,即便是专业的网络人员也要进行繁琐的计算,而IPy模块提供了专门针对IPV4地址与IPV6 ...

  7. 一些webGL地球的网址

    测试浏览器的webgl支持情况:https://browserleaks.com/webgl 或者 https://github.com/AnalyticalGraphicsInc/webglrepo ...

  8. cgroup初步分析(1)

    cgroup的功能和作用不废话,直说一下cgroup的几条设计准则,有了几条设计准则的约束,就比较容易理解其中的数据结构和函数,至于源代码cgroup.c,无非是两个内容,一是task_struct. ...

  9. python 测试报告发送邮件

    使用过程成出现的如下错误 smtplib.SMTPDataError: (554, 'DT:SPM 126 smtp5错误解决办法   1.自动化测试中,调用邮件模块自动发送邮件时,运行脚本报错: s ...

  10. Java变量的修饰符

    1.public public的类.类属变量及方法,包内及包外的任何类均可以访问: 2.protect protected的类.类属变量及方法,包内的任何类,及包外的那些继承了此类的子类才能访问: 3 ...