原题链接在这里:https://leetcode.com/problems/continuous-subarray-sum/description/

题目:

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:

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

Example 2:

Input: [23, 2, 6, 4, 7],  k=6
Output: True
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.

题解:

需要求有没有一段长度大于等于2的subarray的和是k的倍数.

保存之前每个点sum%k的值, 若是又出现了相同的值, 那么这一段subarray的和就是k的倍数.

然后看看这段长度是否大于等于2.

初始化HashMap<Integer, Integer> hm来记录到index i 时的sum%k 和 i的对应关系.

先赋值(0,-1). 原因是sum有包括nums[0]的情况.

这里通过初始index -1 和 i - hm.get(sum%k) > 1来确保.

计算当前值的sum, 若k不是0, sum = sum%k. 看hm中是否已经有过这个余数, 若有看index差是否大于1, 没有就加进hm中.

答案有大于1的return true说明减掉之前的那段正好把余数减光. 若一直没有return false.

Time Complexity: O(nums.length).

Space: O(nums.length).

AC Java:

 class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
hm.put(0,-1); int sum = 0;
for(int i = 0; i<nums.length; i++){
sum += nums[i];
if(k != 0){
sum = sum%k;
} if(hm.containsKey(sum)){
if(i-hm.get(sum)>1){
return true;
}
}else{
hm.put(sum, i);
}
}
return false;
}
}

类似Subarray Sum Equals K.

LeetCode Continuous Subarray Sum的更多相关文章

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

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

  2. LeetCode Continuous Subarray Sum 题解 同余前缀和 Hash表

    文章目录 题意 思路 特殊情况k=0 Source Code 1 Source Code 2 题意 给定一个数组和一个整数k,返回是否存在一个长度至少为2的连续子数组的和为k的倍数. 思路 和上一篇博 ...

  3. 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题是存储的当前累加和的 ...

  4. [LeetCode] 560. Subarray Sum Equals K 子数组和为K

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  5. [LintCode] Continuous Subarray Sum II

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

  6. LintCode 402: Continuous Subarray Sum

    LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...

  7. Continuous Subarray Sum II(LintCode)

    Continuous Subarray Sum II   Given an circular integer array (the next element of the last element i ...

  8. [leetcode]523. Continuous Subarray Sum连续子数组和(为K的倍数)

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

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

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

随机推荐

  1. 最小化CentOS6.7(64bit)---安装mysql5.5、jdk、tomcat

    ********mysql******** ------------------------------------------------------------------------------ ...

  2. Web前端开发人员和设计师必读文章推荐【系列十】

    <Web前端开发人员和设计师必读文章推荐系列十>给大家带来最近两个月发布在<梦想天空>的优秀文章,特别推荐给 Web 开发人员和设计师阅读.梦天空博客关注 前端开发 技术,展示 ...

  3. Oracle数据库的启动与关闭

    一.概述: Oracle数据库的启动分为启动数据库实例.装载数据库和打开数据库3个过程,对应数据库的3种模式. 启动数据库实例:根据数据库初始化参数文件中参数设置,在内存中为数据库分配SGA.PGA等 ...

  4. $用python实现快速排序算法

    本文主要介绍用python实现基本的快速排序算法,体会一下python的快排代码可以写得多么简洁. 1. 三言两语概括算法核心思想 先从待排序的数组中找出一个数作为基准数(取第一个数即可),然后将原来 ...

  5. $《第一行代码:Android》读书笔记——第13章 Android高级技巧

    (一)全局获取Context 1.创建ApplicationUtil类继承自Application类: public class ApplicationUtil extends Application ...

  6. GIT截图

    GIT截图 今天首次成功用了GIT上传了JAVA代码,感觉一下次就能上传这么多代码,确实比在网页上方便.自己一开始根本摸不着头脑,不知道怎样使用GIT软件,但在学姐博客的指导下,在同学热情且耐心地指导 ...

  7. Kubernetes List-Watch

    list-watch,作为k8s系统中统一的异步消息传递方式,对系统的性能.数据一致性起到关键性的作用. list-watch操作需要做这么几件事: 由组件向apiserver而不是etcd发起wat ...

  8. 2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】

    Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  9. 3.mysql多表

    这篇主要总结mysql的建表规则. 01.多表_建表的原则:     需求:创建一个表可以存储学员信息:学员编号,姓名,性别,年龄,科目     1).原则:         1.保证表中的一列,只记 ...

  10. 【P2014】选课(树状DP)

    蒟蒻的第二道树形DP,话说看了这个题的正常做法之后一脸蒙,森林转二叉树??什么诡异的操作,蒟蒻完全没明白那个原理是啥...可能是当初没好好学吧..不管了,索性直接DP. 不难看出,这个题的DP方程和刚 ...