public class Solution {
public int subarraySum(int[] nums, int k) {
int count = 0, pre = 0;
HashMap < Integer, Integer > map = new HashMap < > ();
map.put(0, 1);
for (int i = 0; i < nums.length; i++) {
pre += nums[i];
if (mp.containsKey(pre - k))
count += mp.get(pre - k);
mp.put(pre, mp.getOrDefault(pre, 0) + 1);
}
return count;
}
}

 class Solution {
public int subarraysDivByK(int[] A, int K) {
//记录余数出现的次数
HashMap<Integer, Integer> map = new HashMap<>();
map.put(0,1);
int pre = 0;//前缀和
int ans = 0;
for(int i = 0; i < A.length; i++){
pre += A[i];
int m = (pre % K + K) % K;//余数 (负数处理)
int same = map.getOrDefault(m,0);
ans += same;
map.put(m,same+1);
}
return ans;
}
}

最新文章

  1. 【译】Getting Physical With Memory
  2. phpstorm 10注释的双斜线位置不在缩进的位置:
  3. 优化JS加载时间过长的一种思路
  4. session、cookie
  5. iOS开发流程总结
  6. Java EE开发平台随手记2——Mybatis扩展1
  7. iNeedle日志下载功能问题
  8. postgres 查询数据库所有表名
  9. python 变量命名规范
  10. 无法找到AdbWinApi.dll问题解决 .
  11. MySQL入门笔记
  12. Codeforces 552E - Vanya and Brackets【表达式求值】
  13. Android手势源码浅析-----手势绘制(GestureOverlayView)
  14. APP类别之比较与分析
  15. c# 操作word demo
  16. PyQt QFontDialog显示中文
  17. Nodejs(pkg方式安装)卸载 及命令行模式的重新安装
  18. 【bzoj 3306】树
  19. socketserver 实现并发
  20. J - Romantic

热门文章

  1. CodeForces - 1047A
  2. MySQL 数据库赋权
  3. JWT的浅谈
  4. 如何用Hexo搭建个人博客
  5. aws mysql 开启慢查询日志, 并利用mysqlsla 分析
  6. Objective-C对象模型
  7. Python Tkinter 图形组件介绍
  8. 【Spark】Spark-shell案例——standAlone模式下读取HDFS上存放的文件
  9. NLTK数据包下载频繁报错——解决方法
  10. Boosting算法总结(ada boosting、GBDT、XGBoost)