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:

    1. The length of the array won't exceed 10,000.
    2. You may assume the sum of all the numbers is in the range of a signed 32-bit integer.

这道题给了我们一个数组和一个数字k,让我们求是否存在这样的一个连续的子数组,该子数组的数组之和可以整除k。遇到除法问题,我们肯定不能忘了除数为0的情况等处理。还有就是我们如何能快速的遍历所有的子数组,并且求和,我们肯定不能完全的暴力破解,这样OJ肯定不答应。我们需要适当的优化,如果是刷题老司机的话,遇到这种求子数组或者子矩阵之和的题,应该不难想到要建立累加和数组或者累加和矩阵来做。没错,这道题也得这么做,我们要遍历所有的子数组,然后利用累加和来快速求和。在得到每个子数组之和时,我们先和k比较,如果相同直接返回true,否则再判断,若k不为0,且sum能整除k,同样返回true,最后遍历结束返回false,参见代码如下:

解法一:

class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
for (int i = ; i < nums.size(); ++i) {
int sum = nums[i];
for (int j = i + ; j < nums.size(); ++j) {
sum += nums[j];
if (sum == k) return true;
if (k != && sum % k == ) return true;
}
}
return false;
}
};

下面这种方法用了些技巧,那就是,若数字a和b分别除以数字c,若得到的余数相同,那么(a-b)必定能够整除c。这里就不证明了,博主也不会证明。明白了这条定理,那么我们用一个集合set来保存所有出现过的余数,如果当前的累加和除以k得到的余数在set中已经存在了,那么说明之前必定有一段子数组和可以整除k。需要注意的是k为0的情况,由于无法取余,我们就把当前累加和放入set中。还有就是题目要求子数组至少需要两个数字,那么我们需要一个变量pre来记录之前的和,我们每次存入set中的是pre,而不是当前的累积和,参见代码如下:

解法二:

class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
int n = nums.size(), sum = , pre = ;
unordered_set<int> st;
for (int i = ; i < n; ++i) {
sum += nums[i];
int t = (k == ) ? sum : (sum % k);
if (st.count(t)) return true;
st.insert(pre);
pre = t;
}
return false;
}
};

既然set可以做,一般来说用哈希表也可以做,这里我们建立余数和当前位置之间的映射,由于有了位置信息,我们就不需要pre变量了,之前用保存的坐标和当前位置i比较判断就可以了,参见代码如下:

解法三:

class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
int n = nums.size(), sum = ;
unordered_map<int, int> m{{,-}};
for (int i = ; i < n; ++i) {
sum += nums[i];
int t = (k == ) ? sum : (sum % k);
if (m.count(t)) {
if (i - m[t] > ) return true;
} else m[t] = i;
}
return false;
}
};

参考资料:

https://discuss.leetcode.com/topic/80975/java-solution

https://discuss.leetcode.com/topic/80793/java-o-n-time-o-k-space/2

https://discuss.leetcode.com/topic/80892/concise-c-solution-use-set-instead-of-map

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Continuous Subarray Sum 连续的子数组之和的更多相关文章

  1. [LeetCode] 560. Subarray Sum Equals K 子数组和为K

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

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

  3. [LeetCode] Subarray Sum Equals K 子数组和为K

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

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

  5. [LintCode] Continuous Subarray Sum 连续子数组之和

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

  6. lintcode :continuous subarray sum 连续子数组之和

    题目 连续子数组求和 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.(如果两个相同的答案,请返回其中任意一个) 样例 给定 [-3, ...

  7. LeetCode Continuous Subarray Sum 题解 同余前缀和 Hash表

    文章目录 题意 思路 特殊情况k=0 Source Code 1 Source Code 2 题意 给定一个数组和一个整数k,返回是否存在一个长度至少为2的连续子数组的和为k的倍数. 思路 和上一篇博 ...

  8. LeetCode Continuous Subarray Sum

    原题链接在这里:https://leetcode.com/problems/continuous-subarray-sum/description/ 题目: Given a list of non-n ...

  9. 523. Continuous Subarray Sum

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

随机推荐

  1. android 滑动分页

    import android.app.ListActivity;import android.os.Bundle;import android.os.Handler;import android.vi ...

  2. Android中文API (110) —— CursorTreeAdapter

    前言 本章内容是android.widget.CursorTreeAdapter,版本为Android 3.0 r1,翻译来自"深夜未眠",欢迎访问它的博客:"http: ...

  3. Jquery判断单选框是否选中和获取选中的值

    第一种:利用选中值判断选中 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...

  4. java冒泡排序和快速排序

    本ID技术干货公众号"java工会",欢迎关注指正. 一.冒泡排序 1.算法介绍 设排序表长为n,从后向前或者从前向后两两比较相邻元素的值,如果两者的相对次序不对(A[i-1] & ...

  5. X-pack安装

    1. Install X-Pack into Elasticsearch   docker exec -it anyrobot-store /bin/bash   bin/elasticsearch- ...

  6. 记录python接口自动化测试--unittest框架基本应用(第二目)

    在第一目里写了几个简单demo,并把调用get和post请求的方法封装到了一个类里,这次结合python自带的unittest框架,用之前封装的方法来写一个接口测试demo 1.unittest简单用 ...

  7. oracle导出dmp文件的2种方法

    使用exp和expdp导出数据 1.exp导出数据命令 exp gd_base/@192.168.13.211/oanet file=D:\export\gd_base.dmp log=D:\expo ...

  8. CentOS7安装配置iptables防火墙

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/50779761 CentOS7默认的防火墙不是iptables,而是firewall ...

  9. 在360、UC等浏览器,img不加载原因

    问题:图片在360浏览器不被加载,在UC浏览器强制不显示. 前言不多说,直接上图. 360浏览器显示情况: UC浏览器显示情况: 由以上两张截图可以看到,在360浏览器,banner图片处根本没有加载 ...

  10. 从数据恢复角度解析RAID6结构原理

    [什么是RAID]    RAID的概念描述在互联网上比比皆是,用最简单的原理描述,就是在定义存储方式时允许在一部分数据缺失的情况下不影响全部数据,类似于通讯领域的纠错码.不同的冗余模式形成了不同的R ...