523. Continuous Subarray Sum是否有连续和是某数的几倍
[抄题]:
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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
(a+(n*x))%x is same as (a%x) For e.g. in case of the array [23,2,6,4,7] the running sum is [23,25,31,35,42] and the remainders are [5,1,1,5,0]. We got remainder 5 at index 0 and at index 3. That means, in between these two indexes we must have added a number which is multiple of the k. Hope this clarifies your doubt :)
[一句话思路]:
余数重复,必然增加了几倍
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- pos是之前的index,应该用i - pos是否>1来检测
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[算法思想:递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
//cc
if (nums == null || nums.length == 0) return false;
//ini: map
Map<Integer, Integer> map = new HashMap<Integer, Integer>(){{put(0,-1);}};;
int total_sum = 0;
//for loop: get the same sum
for (int i = 0; i < nums.length; i++) {
total_sum += nums[i];
if (k != 0) total_sum %= k;
Integer pos = map.get(total_sum);
if (pos != null) {
if (i - pos > 1) return true;
}else {
map.put(total_sum, i);
}
}
return false;
}
}
523. Continuous Subarray Sum是否有连续和是某数的几倍的更多相关文章
- 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]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 ...
- 523 Continuous Subarray Sum 非负数组中找到和为K的倍数的连续子数组
非负数组中找到和为K的倍数的连续子数组 详见:https://leetcode.com/problems/continuous-subarray-sum/description/ Java实现: 方法 ...
- 523. Continuous Subarray Sum
class Solution { public: bool checkSubarraySum(vector<int>& nums, int k) { unordered_map&l ...
- 【leetcode】523. Continuous Subarray Sum
题目如下: 解题思路:本题需要用到这么一个数学定理.对于任意三个整数a,b,k(k !=0),如果 a%k = b%k,那么(a-b)%k = 0.利用这个定理,我们可以对数组从头开始进行求和,同时利 ...
- [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] Continuous Subarray Sum 连续的子数组之和
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
随机推荐
- bzoj 3996 [TJOI2015]线性代数——最小割
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3996 b[ i ][ j ] 要计入贡献,当且仅当 a[ i ] = 1 , a[ j ] ...
- erlang学习之自定义behaviour
behaviour是啥,看了资料做了demo以后,感觉像接口,话不多说,祭代码 R15开始,回调模型使用callback来约定,更加好理解了 test_behavior.erl -module(tes ...
- GOF23设计模式之外观模式(facade)
一.外观模式概述 外观模式也称为门面模式. 核心:为了系统提供统一的入口,封装子系统的复杂性,便于客户端调用. 二.外观模式场景导入与示例代码 场景:要想自己去注册一个公司,首先去工商局检测命名是否合 ...
- (转)Linq DataTable的修改和查询
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- mysql的约束
SQL 约束 约束用于限制加入表的数据的类型. 可以在创建表时规定约束(通过 CREATE TABLE 语句),或者在表创建之后也可以(通过 ALTER TABLE 语句). (1)NOT NULL约 ...
- 运维平台cmdb开发-day2
一 发送数据到api(Django的URL) 发送请求携带参数 requests.get(url='http://127.0.0.1:8000/api/asset/?k1=123') # <Qu ...
- 十八 线程暂停 suspend/ resume
1 Suspend.resume 的缺点1 :独占! 线程执行到同步块中,如果线程暂停了,不会释放锁. 比如,比如System.out.println()方法就是一个同步方法, 如果线程调用Sys ...
- dom4J使用笔记
使用dom4j是目前最常用的解析XML的方法,dom4j解析集DOM和SAX技术优点于一身,要使用dom4j 还是先要导入jar: dom4j-1.6.1.jar (dom4j最主要的jar包,可以独 ...
- js内置对象中获取时间的用法--通过date对象获取。
Date对象: var today = new Date(); //年份: var year = today.getFullYear(); //月份 var month = today.getMont ...
- An Autofac Lifetime Primer
Or, “Avoiding Memory Leaks in Managed Composition” Understanding lifetime can be pretty tough when y ...