[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 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.
题目
给定数组和一个数K,求是否存在子数组和为K的倍数。
思路
代码
class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
HashMap<Integer, Integer> map = new HashMap<>();
//为何 map.put(0, -1) 呢? 如果在第2位找到了mod == 0的数,那就 1 -(-1)>1,return true。
map.put(0, -1);
int sum = 0;
for (int i = 0; i < nums.length; i++) {
// running sum
sum += nums[i];
if (k != 0) {
sum %= k;
}
// find sum % k is in the HashMap
if (map.containsKey(sum)) {
// subarray length at least two
if (i - map.get(sum) > 1) {
return true;
}
}
else {
// key: runnng sum -- value: index
map.put(sum, i);
}
}
return false;
}
}
[leetcode]523. Continuous Subarray Sum连续子数组和(为K的倍数)的更多相关文章
- lintcode :continuous subarray sum 连续子数组之和
题目 连续子数组求和 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.(如果两个相同的答案,请返回其中任意一个) 样例 给定 [-3, ...
- [LintCode] Continuous Subarray Sum 连续子数组之和
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 523 Continuous Subarray Sum 非负数组中找到和为K的倍数的连续子数组
非负数组中找到和为K的倍数的连续子数组 详见:https://leetcode.com/problems/continuous-subarray-sum/description/ Java实现: 方法 ...
- 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题是存储的当前累加和的 ...
- 523. Continuous Subarray Sum
class Solution { public: bool checkSubarraySum(vector<int>& nums, int k) { unordered_map&l ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- [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] 209. Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
随机推荐
- C++并发编程 02 数据共享
在<C++并发编程实战>这本书中第3章主要将的是多线程之间的数据共享同步问题.在多线程之间需要进行数据同步的主要是条件竞争. 1 std::lock_guard<std::mute ...
- jshint在bat批处理中闪退,代码中无法调用的问题
先说解决办法:加个call eg: call jshint --version Pause 具体原因有空再更
- angularjs探秘<一>认识angularjs
首先聊聊angularjs是啥. 首先AngularJS 是一个 JavaScript 框架.(PS:其实就是外部引用的js文件) 所以AngularJS的使用依然是外部引用js文件. 附上引用地址 ...
- UVA-550
题意 输入进制数n,第一个乘数的最后一位m,第二个乘数k,乘法的结果为mk, mk的第一位是m,求此时mk的长度 #include<iostream> #include <stdio ...
- 解决Specifying a namespace in include()withou providing an app_name
python3 Django 环境下,如果你遇到namespace没有注册以及在根目录下urls.py中的include方法的第二个参数namespace添加之后就出错的问题.请在[app_name] ...
- 外星人完事了,开始python的matplotlib玩转
外星人完事了,开始python的matplotlib玩转 看书上的例子,在win下安装比较麻烦 今天用pip尝试了一下 pip install matplotlib 然后等待即可 安装完毕后 在pyt ...
- 2. java获取下周日-下周六的时间
String[] arrDate = new String[7]; String[] arrWeek = new String[7]; int mondayPlus = 0; Calendar cd ...
- spring-boot + mybatis +pagehelper 使用分页
转自:https://segmentfault.com/a/1190000015668715?utm_medium=referral&utm_source=tuicool 最近自己搭建一个sp ...
- python 安装mysqldb组件
只能源码安装 源码下载: http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/ http://sourcefor ...
- protocol_link
蔡燧林:1992—2000年教育部考试中心研究生数学命题组组长现在退休养老.要想办法弄到他编的书(ps:别问怎么弄到,我和我同学都能弄到,你怎么会不能弄到呢)李林:目前在导航独家授课,他能屡屡命中考研 ...