原题链接在这里: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. win10系统修改Intel VT-x时进入不了BIOS问题

    一般电脑进入BIOS的方式都是在开机的时候不停的按F2或者F12,但是Win10系统由于支持快速启动,当win10系统快速启动的时候,按F12或者F2是没反应的,解决方式: 第一步:修改win10的启 ...

  2. mysql性能优化的一些建议

    mysql性能优化的一些建议 1.EXPLAIN 你的 SELECT 查询 查看rows列可以让我们找到潜在的性能问题. 2.为关键字段添加索引,比如:where, order by, group b ...

  3. CSS3展开带弹性动画的手风琴菜单

    在线演示 本地下载

  4. 一步一步粗谈linux文件系统(三)----超级块(superblock)【转】

    本文转载自:https://blog.csdn.net/fenglifeng1987/article/details/8302921 超级块是来描述整个文件系统信息的,可以说是一个全局的数据结构,可以 ...

  5. vRA7 Business error “Untrusted certificate chain”

    报错截图: 服务无法注册 第一步:登录vRB 5480页面,取消到vRA的注册 第二部:SSH登录到VRB中,查看bio-ssl.keystore.password. cat /shared/cata ...

  6. JMeter学习(九)分布式部署

    Jmeter 是java 应用,对于CPU和内存的消耗比较大,因此,当需要模拟数以千计的并发用户时,使用单台机器模拟所有的并发用户就有些力不从心,甚至会引起JAVA内存溢出错误.为了让jmeter工具 ...

  7. mysql基础(1)-基本操作

    数据库 数据库(Database,DB)是数据的集合,是一个长期存储在计算机内的.有组织的.有共享的.统一管理的数据集合. 存储数据 管理数据 数据库类型 关系型数据库:由二维表及其之间的联系组成的一 ...

  8. 解决可以Ping通ip地址,但Ping不通域名的思路

    在正常的网络故障处理中,ping命令是大家经常用到的,出现ping通ip地址,但ping域名是出现超时情况,一般是由于TCP/IP协议中的“DNS设置”不正确,请检查其中的配置,或者更换其他可用的DN ...

  9. JMeter接口测试报错,反馈和postman不一样(二)

    我总共现在有两个可以学习的接口,昨天测试一个接口发现问题解决后,今天测试另外一个发现又有问题了 这一次还是反馈显示不一样 要么 这种情况是直接从postman里面拿过来的数据,没做处理  报not j ...

  10. 关于html的一些杂技

    html预定义字符指的是 :<   > html实体指的是 $amp 等 php中htmlspeciachar()就是讲html预定义字符转换成html实体. 浏览器渲染时,会将html实 ...