[抄题]:

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 :)

[一句话思路]:

余数重复,必然增加了几倍

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 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是否有连续和是某数的几倍的更多相关文章

  1. 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题是存储的当前累加和的 ...

  2. [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 ...

  3. 523 Continuous Subarray Sum 非负数组中找到和为K的倍数的连续子数组

    非负数组中找到和为K的倍数的连续子数组 详见:https://leetcode.com/problems/continuous-subarray-sum/description/ Java实现: 方法 ...

  4. 523. Continuous Subarray Sum

    class Solution { public: bool checkSubarraySum(vector<int>& nums, int k) { unordered_map&l ...

  5. 【leetcode】523. Continuous Subarray Sum

    题目如下: 解题思路:本题需要用到这么一个数学定理.对于任意三个整数a,b,k(k !=0),如果 a%k = b%k,那么(a-b)%k = 0.利用这个定理,我们可以对数组从头开始进行求和,同时利 ...

  6. [LintCode] Continuous Subarray Sum II

    Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...

  7. LintCode 402: Continuous Subarray Sum

    LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...

  8. Continuous Subarray Sum II(LintCode)

    Continuous Subarray Sum II   Given an circular integer array (the next element of the last element i ...

  9. [LeetCode] Continuous Subarray Sum 连续的子数组之和

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

随机推荐

  1. yum安装postgresql

    https://wiki.postgresql.org/wiki/YUM_Installation

  2. (转)Android studio 使用心得(五)—代码混淆和破解apk

    这篇文章等是跟大家分享一在Android studio 进行代码混淆配置.之前大家在eclipse上也弄过代码混淆配置,其实一样,大家可以把之前在eclipse上的配置文件直接拿过来用.不管是.cfg ...

  3. Linux GNU C

    Linux 系统上可用的C编译器是GNU C编译器,它建立在自由软件基金会的编程许可证的基础上,因此可以自由发布.GNU C对标准C 进行一系列扩展,以增强标准C的功能. 1.零长度数组GNU C 允 ...

  4. Linux 制作补丁 打补丁 撤销补丁

    1.制作补丁 diff - 逐行比较文件 格式 diff   参数   旧文件/旧文件夹   新文件/新文件夹 -N   将不存在的文件看作是空的 -a   将所有文件都视为文本文件 -u   以合并 ...

  5. 获取响应数据___JSON Extractor 后置处理器

    对于大部分请求返回的结果,都是json,有一个更方便使用的插件:JSON Extractor 不过得首先下载插件 https://jmeter-plugins.org/wiki/JSONPathExt ...

  6. 二、Spark在Windows下的环境搭建

    由于Spark是用Scala来写的,所以Spark对Scala肯定是原生态支持的,因此这里以Scala为主来介绍Spark环境的搭建,主要包括四个步骤,分别是:JDK的安装,Scala的安装,Spar ...

  7. mysql 数据库 自动截取数据的问题---mysql的sql_model的四种模式:宽松模式、严格模式

    mysql支持的sql_mode模式:ANSI.TRADITIONAL.STRICT_ALL_TABLES和STRICT_TRANS_TABLES. ANSI模式:宽松模式,对插入数据进行校验,如果不 ...

  8. 【学徒日记】Unity 动画调用事件

    http://note.youdao.com/noteshare?id=a15f965fc57a0b25c87ee09388cf0f4a 具体内容看上面的链接. 1. 在脚本里写一个函数,它的参数只能 ...

  9. JDBC的复习

    什么是JDBC JDBC(Java DataBase Connectivity)就是Java数据库连接,说白了就是用Java语言来操作数据库.原来我们操作数据库是在控制台使用SQL语句来操作数据库,J ...

  10. 【BZOJ】1926: [Sdoi2010]粟粟的书架(暴力+主席树)

    题目 传送门:QWQ 分析 两道题目 第一问暴力预处理 用$ a[i][j][k] $和$ s[i][j][k] $ 表示从$ (1,1) $ 到 $ (i,j) $ 这个矩形中比k大的数的个数和这些 ...