LeetCode Continuous Subarray Sum
原题链接在这里: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:
- The length of the array won't exceed 10,000.
- 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;
}
}
LeetCode Continuous Subarray Sum的更多相关文章
- [LeetCode] Continuous Subarray Sum 连续的子数组之和
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- LeetCode Continuous Subarray Sum 题解 同余前缀和 Hash表
文章目录 题意 思路 特殊情况k=0 Source Code 1 Source Code 2 题意 给定一个数组和一个整数k,返回是否存在一个长度至少为2的连续子数组的和为k的倍数. 思路 和上一篇博 ...
- 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题是存储的当前累加和的 ...
- [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 ...
- [LintCode] Continuous Subarray Sum II
Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...
- LintCode 402: Continuous Subarray Sum
LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...
- Continuous Subarray Sum II(LintCode)
Continuous Subarray Sum II Given an circular integer array (the next element of the last element i ...
- [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 ...
- [LintCode] Continuous Subarray Sum 连续子数组之和
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
随机推荐
- Python 4 函数的参数,内置函数,装饰器,生成器,迭代器,
一.函数的参数: 1.位置参数:调用函数时根据函数定义的参数位置来传递参数. 2.关键字参数:用于函数调用,通过“键-值”形式加以指定.可以让函数更加清晰.容易使用,同时也清除了参数的顺序需求. 3. ...
- Loadrunder之脚本篇——检查点
VuGen判断脚本是否执行成功是根据服务器返回的状态来确定的,如果服务器返回的是HTTP状态为200 OK,那么VuGen就认为脚本正确地运行了,并且是运行通过的.而大多数系统出错时是不会返回错误页面 ...
- 每天一个Linux命令(44)crontab命令
crontab命令被用来提交和管理用户需要周期性执行的任务,与windows下的计划任务类似. (1)用法: 用法: crontab [-u user] file cron ...
- python cookboo 文件与IO 函数
写出文本数据 g = open('test.txt', 'rt', newline='',encoding = 'utf-8', errors='replace') t是windows平台特有的所谓t ...
- 写python中的装饰器
python中的装饰器主要用于在已有函数实现功能前附加需要输出的信息,下面将用实例展示我如何写装饰器. 首先分别尝试写装饰器装饰一个无参函数和一个有参函数(被装饰函数仅输出,无返回值情况下) def ...
- Django---view视图FBV&CBV
一:创建项目和应用: 或者用命令创建: 1:django-admin.py startproject CBV&FBV 2: cd CBV&FBV (路径切到该文件夹下) 3: pyth ...
- 20165101刘天野 2017-2018-2 《Java程序设计》第8周学习总结
#20165101刘天野 2017-2018-2 <Java程序设计>第8周学习总结 教材学习内容总结 第十二章Java多线程机制 一.进程与线程 1.1.任务调动 大部分操作系统(如Wi ...
- 获取蓝牙mac地址
http://macpu.github.io/2015/11/12/iOS%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%E8%93%9D%E7%89%99Mac%E5%9C ...
- MongoDB快速入门(七)- Save() 方法
MongoDB Save() 方法 save() 方法取代,通过新文档到 save()方法 语法 mongodb 的 save()方法如下所示的基本语法: >db.COLLECTION_NAME ...
- 吴恩达深度学习笔记(五) —— 优化算法:Mini-Batch GD、Momentum、RMSprop、Adam、学习率衰减
主要内容: 一.Mini-Batch Gradient descent 二.Momentum 四.RMSprop 五.Adam 六.优化算法性能比较 七.学习率衰减 一.Mini-Batch Grad ...