原题链接在这里: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. body-parser小解

    body-parser用来解析http请求体,对不同的content-type有不同的处理方式, 首先介绍一下常见的四种Content-Type: 1.application/x-www-form-u ...

  2. iOS_核心动画CALayer(一)

    目 录: 一.核心动画简介 二.图层与视图之间的关系 三.CALayer的使用说明 四.CALayer的隐式动画属性 五.在CALayer上绘图 六.总结     一.核心动画简介 Core Anim ...

  3. INSPIRED启示录 读书笔记 - 第12章 产品探索

    软件项目可以划分为两个阶段 探索产品阶段:弄清楚要开发什么产品(定义正确的产品) 在探索产品的阶段,产品经理负责分析各种创意,广泛收集用户需求,了解如何运用新技术,拿出产品原型并加以测试 从全局视角思 ...

  4. win10 x64下的DNW驱动不完全安装方法【转】

    本文转载自:https://blog.csdn.net/sihaiwenshu/article/details/52503550 一.起因 最新心血来潮想学ARM,JZ2440开发板买回来后就开始折腾 ...

  5. CentOS7安装 VirtualBox虚拟机

    官方地址  : https://www.virtualbox.org/wiki/Linux_Downloads 1.导入 yum 源 Oracle Linux / RHEL #cd /etc/yum. ...

  6. Spring MVC 接收多个实体参数

    在SpringMVC 的接收参数中,如果接收一个实体对象,只需要在方法参数中这样做:@RequestBody User user //单个的时候这样接收 @RequestMapping(value = ...

  7. space sniffer清理的空间

    部分超级大的单文件,比如数据库 C:\inetpub\logs\LogFiles\W3SVC4 C:\Users\clu\AppData\Local\JetBrains\Transient C:\Us ...

  8. Apache Phoenix的Array类型

    Apache Phoenix支持JDBC ARRAY类型,任何原生的数据类型就可以在ARRAY中使用.下面我介绍一下在创建的表中使用ARRAY类型. 先看一下创建表的SQL语句: CREATE TAB ...

  9. Kafka详解一:Kafka简介

    问题导读 1.Kafka有何特性?2.Kafka有哪些组件? 背景:     当今社会各种应用系统诸如商业.社交.搜索.浏览等像信息工厂一样不断的生产出各种信息,在大数据时代,我们面临如下几个挑战: ...

  10. Qt 安装事件过滤器installEventFilter

    Qt 安装事件过滤器installEventFilter (2013-01-28 14:29:18) 转载▼   分类: 工作笔记 Qt的事件模型一个强大的功能是一个QObject对象能够监视发送其他 ...