[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 ...
随机推荐
- [UE4]C++ 动态内存分配(6种情况,好几个例子)
1.堆内存分配 : C/C++定义了4个内存区间: 代码区,全局变量与静态变量区,局部变量区即栈区,动态存储区,即堆(heap)区或自由存储区(free store). 堆的概念: 通常定义变量(或对 ...
- 战争迷雾Fog Of War
参考:https://forums.unrealengine.com/community/community-content-tools-and-tutorials/26436-tutorial-fo ...
- Ubuntu12.10下Vsftpd的安装
安装Vsftpd sudo apt-get install vsftpd 配置 sudo vim /etc/vsftpd.conf 取消以下两行前面的注释 local_enable=YES write ...
- storm项目优化
实现监控脚本监控topology运行状态
- 基于Linux的Samba开源共享解决方案测试(五)
对于客户端的网络监控如图: 双NAS网关50Mb码率视音频文件的稳定写测试结果如下: 100Mb/s负载性能记录 NAS网关资源占用 稳定写 稳定写 CPU空闲 内存空闲 网卡占用 NAS1 16个稳 ...
- Python数据分析_Pandas01_数据框的创建和选取
主要内容: 创建数据表 查看数据表 数据表索引.选取部分数据 通过标签选取.loc 多重索引选取 位置选取.iloc 布尔索引 Object Creation 新建数据 用list建series序列 ...
- PHP获取跳转后的URL,存到数据库,设置缓存时间
<?php error_reporting(0); header("Content-Type: text/html; charset=utf-8"); $fid=$_GET[ ...
- idea建立一个maven项目
前言:虽然之前实习的时候有过spring框架的实践,但是因为基本上都是在已有的基础上进行修修补补,并没有对Spring框架有深刻的理解.所以从今天起,要从零开始对Spring框架进行学习.在此之前,第 ...
- 9.求背景图片左边到#box盒子左边外框侧的距离
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- mime设置
ie9对mime有特殊要求,必须要有type才可以. 如果出现css的mime类型不支持.则没有加 type="css/text" 查看本机的mime支持: regedit > ...