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. LAMP平台部署(转)

    LAMP平台的概述 LAMP环境脚本部署:https://github.com/spdir/ShellScripts/tree/master/lamp LAMP的介绍:百度百科 LAMP平台的构成组件 ...

  2. java基础笔记(4)----数组

    介绍: 数组是一种数据类型,是引用类型,是一块连续的内存空间,用于存储和管理相同类型的多个数据. 定义:-- > 数组的声明方式 先声明,在开辟内存空间--> int [] a; a=ne ...

  3. Java基础学习笔记四 Java基础语法

    数组 数组的需求 现在需要统计某公司员工的工资情况,例如计算平均工资.最高工资等.假设该公司有50名员工,用前面所学的知识完成,那么程序首先需要声明50个变量来分别记住每位员工的工资,这样做会显得很麻 ...

  4. (译文)开始学习Vue.js特性--Scoped Slots

    什么是scoped slots A scoped slot is a special type of slot that functions as a reusable template (that ...

  5. drbd(三):drbd的状态说明

    本文目录:1.drbd配置文件2.状态 2.1 连接状态(connect state,cs)和复制状态 2.2 角色状态(roles,ro) 2.3 磁盘状态(disk state,ds) 2.4 I ...

  6. C语言程序设计基础-第1周作业-初步

    1.安装带有计算机术语的翻译软件 2.在自己电脑上安装C编译器,windows系统建议安装dev-c++,其他系统自行查找. 3.加入课程小组,有任何疑问可以在小组中提问:https://group. ...

  7. 使用Python定制词云

    一.实验介绍 1.1 实验内容 在互联网时代,人们获取信息的途径多种多样,大量的信息涌入到人们的视线中.如何从浩如烟海的信息中提炼出关键信息,滤除垃圾信息,一直是现代人关注的问题.在这个信息爆炸的时代 ...

  8. hdu 4553 约会安排

    约会安排 http://acm.hdu.edu.cn/showproblem.php?pid=4553 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  9. C#中的函数式编程:递归与纯函数(二)

    在序言中,我们提到函数式编程的两大特征:无副作用.函数是第一公民.现在,我们先来深入第一个特征:无副作用. 无副作用是通过引用透明(Referential transparency)来定义的.如果一个 ...

  10. zookeeper 入门系列-理论基础 – zab 协议

    上一章讨论了paxos算法,把paxos推到一个很高的位置.但是,paxos有没有什么问题呢?实际上,paxos还是有其自身的缺点的: 1. 活锁问题.在base-paxos算法中,不存在leader ...