题目如下:

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

解题思路:本题需要用到一个数学规律,如果a%c = b%c,那么(a-b)%c=0。我的解法就是从后往前遍历数组,依次累加每个元素的值并记为sum,同时用字典保存sum%K作为key值出现的次数。同时每累加一个元素,只要去字典中查找历史sum%K出现的次数,这个次数就是从以这个元素作为起点满足条件的子数组的个数。特别注意的是,如果sum%K=0,那么表示这个元素本身就满足条件,次数要+1。

代码如下:

class Solution(object):
def subarraysDivByK(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: int
"""
dic = {}
count = 0
res = 0
for i in A[::-1]:
count += i
if count%K in dic:
res += dic[count%K]
if count % K == 0:
res += 1
dic[count%K] = dic.setdefault(count%K,0)+1
return res

【leetcode】974. 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(Java)

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

  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. 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 【LeetCode】713. Subarray Product Less Than K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/subarray ...

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

  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. 【leetcode】1022. Smallest Integer Divisible by K

    题目如下: Given a positive integer K, you need find the smallest positive integer N such that N is divis ...

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

随机推荐

  1. boost compressedPair

    boost::compressed_pair behaves like std::pair. However, if one or both template parameters are empty ...

  2. Hadoop编程调用HDFS(JAVA)

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 Hadoop环境: Cloudera QuickStart 2.GITHUB地址 http ...

  3. Delphi读取和写入utf-8编码格式的文件

    读取UTF-8格式的文件内容 function LoadUTF8File(AFileName: string): string; var ffileStream:TFileStream; fAnsiB ...

  4. pytest_用例运行级别_class级

    ''' 模块级(setup_module/teardown_module)开始于模块始末, 全局的在类中不起作用 类级(setup_class/teardown_class)只在类中前后运行一次(在 ...

  5. display:inline-block在IE6/Ie7和IE8中的区别

    在IE6.IE7中不识别display:inline-block属性,但使用inline-block属性在IE下会触发layout,从而使内联元素拥有了display:inline-block属性的表 ...

  6. drf基础

    1.什么是编程? 数据结构和算法的结合 2.什么是REST? 同一个功能会产生五花八门的url(把查看单条记录和查看多条记录都看成是一个功能),而且响应回去的数据也没有同一的格式规范,这就造成了前后端 ...

  7. 转 用SQL语句,删除掉重复项只保留一条

    用SQL语句,删除掉重复项只保留一条 用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢1.查找表中多余的重复记录,重复记录是根据单个字段(p ...

  8. C++中的智能指针类模板

    1,智能指针本质上是一个对象,这个对象可以像原生的指针一样使用,因为智能指 针相关的类通过重载的技术将指针相关的操作符都进行了重载,所以智能指针对象可以像原生指针一样操作,今天学习智能指针类模板,通过 ...

  9. XMLSpy 生成xml模板(转)

    公司中的生成ci需要和xsd中的sequence一致, 由于xsd的过于庞大,且有继承关系, 所以人工比较是不可能的. 现用xmlspy来生成. 1, 在xmlspy中打开xsd 2, 将choice ...

  10. 如何通过HTTP API 调取tushare的股票数据

    长久以来,Tushare一直以固定的Python SDK方式为大家提供数据服务. 虽然在基于Python的数据分析和Python的量化策略开发很方便,但习惯用其他语言的同学们表示了“抗议”,于是在Tu ...