-
-
Notifications
You must be signed in to change notification settings - Fork 737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[LeetCode] 523. Continuous Subarray Sum #523
Comments
Simple proofa % c == b % c so (a - b) % c == 0 |
您好,我已收到您的邮件,会尽快回复!
|
已添加,多谢提供~ |
@grandyang I am so sorry, please correct my proof. - a - b = mc + d - (nc + d) = (m + n) * c
+ a - b = mc + d - (nc + d) = (m - n) * c Anyway, Thanks for your leetcode solutions repository, it's very helpful for me. |
OK, thanks! |
Given an integer array
nums
and an integerk
, returntrue
ifnums
has a continuous subarray of size at least two whose elements sum up to a multiple ofk
, orfalse
otherwise.An integer
x
is a multiple ofk
if there exists an integern
such thatx = n * k
.0
is always a multiple ofk
.Example 1:
Example 2:
Example 3:
Constraints:
1 <= nums.length <= 105
0 <= nums[i] <= 109
0 <= sum(nums[i]) <= 231 - 1
1 <= k <= 231 - 1
这道题给了我们一个数组和一个数字k,让求是否存在这样的一个连续的子数组,该子数组的数组之和可以整除k。遇到除法问题,肯定不能忘了除数为0的情况等处理。还有就是如何能快速的遍历所有的子数组,并且求和,这里肯定不能完全的暴力破解,OJ 肯定不答应。需要适当的优化,如果是刷题老司机的话,遇到这种求子数组或者子矩阵之和的题,应该不难想到要建立累加和数组或者累加和矩阵来做。没错,这道题也得这么做,我们要遍历所有的子数组,然后利用累加和来快速求和。在得到每个子数组之和时,先和k比较,如果相同直接返回 true,否则再判断,若k不为0,且 sum 能整除k,同样返回 true,最后遍历结束返回 false,参见代码如下(不过貌似这种方法现在已经超时了):
解法一:
下面这种方法用了些技巧,那就是,若数字a和b分别除以数字c,若得到的余数相同,那么 (a-b) 必定能够整除c。感谢热心网友 iChenLei 提供的简易证明:
a % c == b % c
a = mc + d;
b = nc + d;
a - b = mc + d - (nc + d) = (m - n) * c
so (a - b) % c == 0
明白了这条定理,用一个集合 HashSet 来保存所有出现过的余数,如果当前的累加和除以k得到的余数在 HashSet 中已经存在了,那么说明之前必定有一段子数组和可以整除k。需要注意的是k为0的情况,由于无法取余,就把当前累加和放入 HashSet 中。还有就是题目要求子数组至少需要两个数字,那么需要一个变量 pre 来记录之前的和,每次存入 HashSet 中的是 pre,而不是当前的累积和,参见代码如下:
解法二:
既然 HashSet 可以做,一般来说用 HashMap 也可以做,这里我们建立余数和当前位置之间的映射,由于有了位置信息,就不需要 pre 变量了,之前用保存的坐标和当前位置i比较判断就可以了,参见代码如下:
解法三:
Github 同步地址:
#523
参考资料:
https://leetcode.com/problems/continuous-subarray-sum/
https://leetcode.com/problems/continuous-subarray-sum/discuss/99567/java-solution
https://leetcode.com/problems/continuous-subarray-sum/discuss/99499/java-on-time-ok-space
https://leetcode.com/problems/continuous-subarray-sum/discuss/99506/concise-c-solution-use-set-instead-of-map
LeetCode All in One 题目讲解汇总(持续更新中...)
喜欢请点赞,疼爱请打赏❤️~.~
微信打赏
|
Venmo 打赏
---|---
The text was updated successfully, but these errors were encountered: