分析

这题场上前缀和都想出来了,然后就没有然后了。。。哭惹.jpg

前缀和相减能够得到任意一段连续区间的和,然后他们取余\(K\)看余数是否为0就能得到。这是朴素的遍历算法。那么反过来说,如果两个前缀和都对\(K\)余\(p\),那么他们相减一定能够被K整除。

我们就这么统计就可以了,然后一个简单的求和即可。想到不难,难的是想到。

代码

class Solution {
public int subarraysDivByK(int[] A, int K) {
int[] prefix = new int[A.length+1];
for(int i=0; i<A.length; ++i)
{
prefix[i+1] = prefix[i] + A[i];
}
int[] cnt = new int[K];
for(int p: prefix)
{
cnt[(p%K+K)%K]++;
}
int ans=0;
for(int c: cnt)
{
ans += c*(c-1)/2;
}
return ans;
}
}

「Leetcode」974. Subarray Sums Divisible by K(Java)的更多相关文章

  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. 974. Subarray Sums Divisible by K

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

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

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

  6. Leetcode 974. Subarray Sums Divisible by K

    前缀和(prefix sum/cumulative sum)的应用. 还用了一个知识点: a≡b(mod d) 则 a-b被d整除. 即:a与b对d同余,则a-b被d整除. class Solutio ...

  7. 「LeetCode」0952-Largest Component Size by Common Factor(Go)

    分析 注意到要求的是最大的连通分量,那么我们可以先打素数表(唯一分解定理),然后对每个要求的数,将他们同分解出的质因子相连(维护一个并查集),然后求出最大的联通分量即可. 这里使用了筛法求素数.初始化 ...

  8. 「翻译」Unity中的AssetBundle详解(二)

    为AssetBundles准备资源 使用AssetBundles时,您可以随意将任何Asset分配给所需的任何Bundle.但是,在设置Bundles时,需要考虑一些策略.这些分组策略可以使用到任何你 ...

  9. 「翻译」Unity中的AssetBundle详解(一)

    AssetBundles AssetBundle是一个存档文件,其中包含平台在运行时加载的特定资产(模型,纹理,预制,音频剪辑,甚至整个场景).AssetBundles可以表示彼此之间的依赖关系;例如 ...

随机推荐

  1. coco定义的小物体中物体大物体的尺寸

    http://cocodataset.org/#detection-leaderboard

  2. 初始化列表initializer_list

    初始化列表定义在<initializer_list>,初始化列表简化了参数数量可变的函数的编写,初始化列表的所有的元素都应该是同一种数据类型 由于定义了列表中允许的类型,所以初始化列表是安 ...

  3. linux(Centos系统)部署项目(vue+nginx+tomcat)

    条件,在服务器安装好tomcat,nginx; 安装nginx命令:# yum install nginx 启动Nginx命令:# systemctl start nginx.service 给权限 ...

  4. Eclipse查看.properties文件中文乱码

    在中文操作系统中,Eclipse中的Java类型文件的编码的默认设置是GBK,但是对Properties资源文件的编码的默认设置是ISO-8859-1.所以编辑Java文件中的中文不会出现问题,但编辑 ...

  5. 指定Android adb的启动端口

    串口执行: setprop service.adb.tcp.port stop adbd start adbd 一般机器默认是5555为adb端口,但是今天遇到的一台机器以5037为默认端口,开发机器 ...

  6. WAPM环境配置(PHP入门2)

    WAMP介绍 W:Window:开源免费 A:Apache:开源免费 M:MySQL:开源免费 P:PHP:开源免费 Apache下载 Apache下载地址:https://www.apachehau ...

  7. java 进销存 库存管理 销售报表 商户管理 springmvc SSM crm 项目

    系统介绍: 1.系统采用主流的 SSM 框架 jsp JSTL bootstrap html5 (PC浏览器使用) 2.springmvc +spring4.3.7+ mybaits3.3  SSM ...

  8. 推荐一个Oracle数据库学习网站

    推荐一个我个人的Oracle数据库学习网站,比较系统性的整理,会持续更新的网站.网址: Oracle基础教程: http://www.oraclejsq.com/article/010100110.h ...

  9. NOIP模拟赛D2T1自己的解题思路

    T1题目在此: 数轴上有n个球,每个球直径为1,第 ii 个球的左端点为pi即占据了数轴上[pi,pi+1][pi,pi+1]).在 P位置有一堵墙.有q个操作,每次要么以x位置为左端点放一个新球(如 ...

  10. ubuntu18.10手动安装mysql5.5

    一.问题描述 本人使用的电脑安装的是ubuntu18.10系统,使用aptitude install mysql-server命令安装mysql时,总是无法正常安装,无法正常配置mysql-serve ...