题目如下:

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 = 0res = 0for i in A[::-1]:count += iif count%K in dic:res += dic[count%K]if count % K == 0:res += 1dic[count%K] = dic.setdefault(count%K,0)+1return res

转载于:https://www.cnblogs.com/seyjs/p/10317990.html

【leetcode】974. Subarray Sums Divisible by K相关推荐

  1. LeetCode:974. Subarray Sums Divisible by K - Python

    974. 和可被 K 整除的子数组 问题描述: 给定一个整数数组 A,返回其中元素之和可被K整除的(连续.非空)子数组的数目. 示例: 输入:A = [4,5,0,-2,-3,1], K = 5 输出 ...

  2. leetcode 974 Subarray Sums Divisible by K

    leetcode 974 Subarray Sums Divisible by K 1.题目描述 2.解题思路 3.Python代码 1.题目描述 给定一个整数数组 A,返回其中元素之和可被 K 整除 ...

  3. leetcode 974. Subarray Sums Divisible by K

    974. Subarray Sums Divisible by K 题意:给你一个数组A和一个数K,求改数组有多少连续子序列的和能被K整除. 思路:简单DP.假设sum[i]表示[0-i]的求和,那么 ...

  4. 【LeetCode】713. Subarray Product Less Than K 乘积小于 K 的子数组

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 文章目录 题目描述 题目大意 解题方法 滑动窗口 复杂度 分享滑动窗口模板 总结 日期 ...

  5. leetcode 974. Subarray Sums Divisible by K的解法(统计共同余数)

    题目大意:给定一个整数数组 A,返回其中元素之和可被 K 整除的(连续.非空)子数组的数目 大致思路:比如数组A = [1,2,1], K=2,那么1%2 =1,(1+2)%2=1,所以 {2}是符合 ...

  6. 974. Subarray Sums Divisible by K

    Title 给定一个整数数组 A,返回其中元素之和可被 K 整除的(连续.非空)子数组的数目. 示例: 输入:A = [4,5,0,-2,-3,1], K = 5 输出:7 解释: 有 7 个子数组满 ...

  7. 974. Subarray Sums Divisible by K [Medium]

    用了prefix sum的思想,之前没用过,感觉很难 /*** Runtime: 18 ms, faster than 56.05%* Memory Usage: 42.9 MB, less than ...

  8. LeetCode 974. Subarray Sums Divisible by K--Python解法--数学题--取模求余

    LeetCode 974. Subarray Sums Divisible by K–Python解法–数学题–取模求余 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结 ...

  9. 【leetcode】1018. Binary Prefix Divisible By 5

    题目如下: Given an array A of 0s and 1s, consider N_i: the i-th subarray from A[0] to A[i] interpreted a ...

最新文章

  1. Python使用matplotlib进行3D可视化分析:3d柱状图、3d直方图、3d线框图、3d曲面图、3d翼面图(莫比乌斯环)
  2. git使用--提交代码
  3. CodeForces - 1331E Jordan Smiley(图形转换)
  4. Java多线程 ——线程基础和锁锁锁
  5. 数据的存储特殊矩阵压缩存储【数据结构F】
  6. Ubuntu查看文件夹下文件的个数
  7. 英特尔发布至强E-2300服务器处理器,比上一代性能提高17%
  8. 阿里影业宣布新战略:“新基础设施”赋能电影产业
  9. ansible 建 kubernetes 证书签名请求_Kubernetes 两步验证 - 使用 Serverless 实现动态准入控制
  10. android 知识点大全 面试
  11. 基于讯飞语音API应用开发之——离线词典构建
  12. 服务器pci光卡 系统不显示,工控机系统读取不到PCI卡的原因及解决办法
  13. 腾讯面试:我倒在了网络基础知识
  14. Win10重复按键盘经常按不出?Win10关闭筛选键步骤
  15. 微信小程序之授权登录的实现(button按钮)
  16. Clover使用笔记(持续更新)
  17. 分享多张图片到微信朋友圈
  18. Tornado框架简介
  19. 随笔——网易云的神评论(持续更新)
  20. 【无标题】C语言 最简单的代码

热门文章

  1. 数学问题的解题方法(模板)
  2. 天天动听 悬浮歌词(迷你歌词)效果解读
  3. npm如何删除node_modules文件夹
  4. mybatis3+struts2+oracle整合的一个好的demo
  5. 字符串charAt()
  6. 驱动学习之LED驱动框架
  7. c++中explict关键字
  8. Android 动画小知识点
  9. jquery中offset和position的区别
  10. C#算法大全-1-Hanoi