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 ...
随机推荐
- tomcat深入学习
总体结构:https://www.jianshu.com/p/d74eef07487f servlet相关:https://www.ibm.com/developerworks/cn/java/j-l ...
- Lombok 简单入门
原文地址:Lombok 简单入门 博客地址:http://www.extlight.com 一.前言 Lombok 是一个 Java 库,它作为插件安装至编辑器中,其作用是通过简单注解来精简代码,以此 ...
- oracle 卸载操作
1. 用 oracle 用户登录 如果要再次安装, 最好先做一些备份工作. 包括用户的登录脚本,数据库自动启动关闭的脚本,和 Listener 自动启动的脚本. 要是有可能连创建数据库的脚本也保存下来 ...
- DS05--查找
一.学习总结 1.查找的思维导图 2.查找学习体会 2.1 关联容器和顺序容器 c++中有两种类型的容器:顺序容器和关联容器,顺序容器主要有:vector.list.deque等.其中vector表示 ...
- postman参数化的方法
1.准备csv格式的文件(注意第一行是是引用参数的名称) 2.编写请求,应用变量参数,并且设置断言 引用变量参数 3.把这个请求的文件夹runner一下批量执行 4.把第一步变量的csv文件在runn ...
- css:层叠样式表-网页布局基础
css:层叠样式表:一.写法分类: 1.内联(行内,写在标签里面,以属性的形式表现,属性名是style) 例:<table style="background-color: aqua& ...
- python‘s second day for me
in not in 主要用来检测一些字符串是否存在,或者避免一些字符串 while True: comment = input('请输入你的评论') if '顾清秋' in comment: ...
- find命令中的print0和xargs -0
看到命令find . -name checkout-cache -f -- 不明白其中-print0和 xargs -0的用法.查了一下,转载一篇备忘. xargs命令的作用是将参数列表转换成小块分段 ...
- Tkinter LabelFrame
Tkinter LabelFrame: 在一个labelframe一个简单的容器构件.其主要目的是作为一个间隔或复杂的窗口布局容器. 在一个labelframe一个简单的容器构件.其主要目的是作 ...
- JS 对输入框文本正在输入中校验
// keyup getInput.keyup(function(){ var a = parseInt(getPrice); var b = parseInt(getInput.val()); // ...