Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.

Example 1:

  1. Input: [23, 2, 4, 6, 7], k=6
  2. Output: True
  3. Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.

Example 2:

  1. Input: [23, 2, 6, 4, 7], k=6
  2. Output: True
  3. Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.

Note:

  1. The length of the array won't exceed 10,000.
  2. You may assume the sum of all the numbers is in the range of a signed 32-bit integer.

题目

给定数组和一个数K,求是否存在子数组和为K的倍数。

思路

代码

  1. class Solution {
  2. public boolean checkSubarraySum(int[] nums, int k) {
  3. HashMap<Integer, Integer> map = new HashMap<>();
  4. //为何 map.put(0, -1) 呢? 如果在第2位找到了mod == 0的数,那就 1 -(-1)>1,return true。
  5. map.put(0, -1);
  6. int sum = 0;
  7. for (int i = 0; i < nums.length; i++) {
  8. // running sum
  9. sum += nums[i];
  10. if (k != 0) {
  11. sum %= k;
  12. }
  13. // find sum % k is in the HashMap
  14. if (map.containsKey(sum)) {
  15. // subarray length at least two
  16. if (i - map.get(sum) > 1) {
  17. return true;
  18. }
  19. }
  20. else {
  21. // key: runnng sum -- value: index
  22. map.put(sum, i);
  23. }
  24. }
  25. return false;
  26. }
  27. }

[leetcode]523. Continuous Subarray Sum连续子数组和(为K的倍数)的更多相关文章

  1. lintcode :continuous subarray sum 连续子数组之和

    题目 连续子数组求和 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.(如果两个相同的答案,请返回其中任意一个) 样例 给定 [-3, ...

  2. [LintCode] Continuous Subarray Sum 连续子数组之和

    Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...

  3. [LeetCode] Minimum Size Subarray Sum 最短子数组之和

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  4. 523 Continuous Subarray Sum 非负数组中找到和为K的倍数的连续子数组

    非负数组中找到和为K的倍数的连续子数组 详见:https://leetcode.com/problems/continuous-subarray-sum/description/ Java实现: 方法 ...

  5. leetcode 560. Subarray Sum Equals K 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)

    整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...

  6. 523. Continuous Subarray Sum

    class Solution { public: bool checkSubarraySum(vector<int>& nums, int k) { unordered_map&l ...

  7. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  8. [LeetCode] Continuous Subarray Sum 连续的子数组之和

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

  9. [LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和

    Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...

随机推荐

  1. Oracle数据库的一些视图

    1.之前一直是SQL使用,没有深入学习数据库的底层知识,尤其使其进程以及锁以及底层的数据如何存储的这些知识. 2.渐渐的在开发中对数据库的要求慢慢高了 比如:临时表 -----BI里面,存储过程里面 ...

  2. HIbernate编程模型

    1.Hibernate: ORM框架,简化SQL开发,编程接口丰富,简化JDBC编程 2.有点: Lazy机制配合Fetch的HQL高级查询,提高开发效率 难点:理解Lazy与Fetch JOIN的原 ...

  3. 学大伟业Day1解题报告

    学大伟业Day1解题报告 张炳琪 一.   时间分配 T1:30分钟  T2: 60分钟  T3:100分钟 二.答题情况及错因 T1:100         T2:55             T3 ...

  4. 本地git仓库常用操作

    SSH配置: 本机创建SSH key $ ssh-keygen -t rsa -C "youremail@example.com" 将SSHkey添加到git仓库:id_rsa.p ...

  5. 0_Simple__simplePitchLinearTexture

    对比设备线性二维数组和 CUDA 二维数组在纹理引用中的效率 ▶ 源代码.分别绑定相同大小的设备线性二维数组和 CUDA 二维数组为纹理引用,做简单的平移操作,重复若干次计算带宽和访问速度. #inc ...

  6. 0_Simple__simpleCubemapTexture

    立方体纹理贴图 ▶ 源代码.用纹理方法把元素按原顺序从 CUDA3D 数组中取出来,求个相反数放入全局内存,输出. #include <stdio.h> #include "cu ...

  7. Java编程最差实践

    原文地址:http://www.odi.ch/prog/design/newbies.php 每天在写Java程序, 其实里面有一些细节大家可能没怎么注意, 这不, 有人总结了一个我们编程中常见的问题 ...

  8. 迷你MVVM框架 avalonjs 学习教程17、avalon的一些配置项

    本章节,主要是介绍avalon.config方法,通过它来制定一些更贴心的功能. 一般情况下,我们在使用ms-controller绑定时,需要添加一个ms-controller类名,目的是为了防止网速 ...

  9. MySql重复查询

    MYSQL查询重复记录的方法很多,下面就为您介绍几种最常用的MYSQL查询重复记录的方法,希望对您学习MYSQL查询重复记录方面能有所帮助. 1.查找表中多余的重复记录,重复记录是根据单个字段(peo ...

  10. Unknown column in 'field list'

    好久都没有亲手写数据库的连接以及操作了,近期一直都是用ejb,直接就映射到数据库了,所以关于jdbc的知识都忘得差不多了.不过吧,为了学习struts2,还是重新将jdbc知识捡起来.找了一上午的错误 ...