Skip to content

Commit 1594ff1

Browse files
committed
完成523
1 parent 5c9daee commit 1594ff1

File tree

10 files changed

+71
-1
lines changed

10 files changed

+71
-1
lines changed

docs/0000-00-note.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@
22

33
. xref:0215-kth-largest-element-in-an-array.adoc[215. Kth Largest Element in an Array] 没想到快速排序的分区算法,竟然可以用于做快速选择?!神奇… 可惜的是,这题目以前做过,现在都给忘完了…
44
. xref:0437-path-sum-iii.adoc[437. Path Sum III] 前缀和的解法还需要再思考思考!
5+
6+
== 解题技巧
7+
8+
. 树形 DP 套路
9+
. 前缀和
10+
.. xref:0523-continuous-subarray-sum.adoc[523. Continuous Subarray Sum]

docs/0523-continuous-subarray-sum.adoc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
[#0523-continuous-subarray-sum]
12
= 523. Continuous Subarray Sum
23

34
https://leetcode.com/problems/continuous-subarray-sum/[LeetCode - Continuous Subarray Sum]
@@ -33,6 +34,24 @@ Given a list of *non-negative* numbers and a target *integer* k, write a functio
3334
. You may assume the sum of all the numbers is in the range of a signed 32-bit integer.
3435

3536

37+
== 解题分析
38+
39+
利用同余定理:
40+
41+
image::images/0523-01.png[]
42+
43+
当 latexmath:[prefixSums[q\]−prefixSums[p\]] 为 latexmath:[k] 的倍数时,latexmath:[prefixSums[p\]] 和 latexmath:[prefixSums[q\]] 除以 latexmath:[k] 的余数相同。(_D瓜哥注:余数相同,则相减之后余数就被减掉了。_)因此只需要计算每个下标对应的前缀和除以 latexmath:[k] 的余数即可,使用哈希表存储每个余数第一次出现的下标。
44+
45+
image::images/0523-02.png[]
46+
47+
image::images/0523-03.png[]
48+
49+
image::images/0523-04.png[]
50+
51+
image::images/0523-05.png[]
52+
53+
image::images/0523-06.png[]
54+
3655

3756

3857
[[src-0523]]

docs/images/0523-01.png

27.4 KB
Loading

docs/images/0523-02.png

15 KB
Loading

docs/images/0523-03.png

31.8 KB
Loading

docs/images/0523-04.png

15.1 KB
Loading

docs/images/0523-05.png

32 KB
Loading

docs/images/0523-06.png

36.1 KB
Loading

src/main/java/com/diguage/algorithm/leetcode/_0110_BalancedBinaryTree_21.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* https://leetcode.com/problems/balanced-binary-tree/[LeetCode - Balanced Binary Tree]
1212
*
13-
* @author D瓜哥, https://www.diguage.com/
13+
* @author D瓜哥 · https://www.diguage.com
1414
* @since 2020-02-06 23:10
1515
*/
1616
public class _0110_BalancedBinaryTree_21 {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.diguage.algorithm.leetcode;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2024-06-23 00:23:40
9+
*/
10+
11+
public class _0523_ContinuousSubarraySum {
12+
/**
13+
* 参考 https://leetcode.cn/problems/continuous-subarray-sum/solutions/807930/lian-xu-de-zi-shu-zu-he-by-leetcode-solu-rdzi/[523. 连续的子数组和 - 官方题解^]
14+
*/
15+
public boolean checkSubarraySum(int[] nums, int k) {
16+
if (nums == null || nums.length < 2) {
17+
return false;
18+
}
19+
Map<Integer, Integer> remainderToIndexMap = new HashMap<>();
20+
// 当 sum=0 时,还没有任何数字参与,所以是 -1
21+
remainderToIndexMap.put(0, -1);
22+
int remainder = 0;
23+
for (int i = 0; i < nums.length; i++) {
24+
// 同余定理
25+
remainder = (remainder + nums[i]) % k;
26+
if (remainderToIndexMap.containsKey(remainder)) {
27+
int prevIndex = remainderToIndexMap.get(remainder);
28+
if (i - prevIndex >= 2) {
29+
return true;
30+
}
31+
} else {
32+
// 前面已经判断过是否包含该值,
33+
// 这里也就不需要为了保存最小的下标去判断是否已经存在该值
34+
remainderToIndexMap.put(remainder, i);
35+
}
36+
}
37+
return false;
38+
}
39+
40+
public static void main(String[] args) {
41+
_0523_ContinuousSubarraySum solution = new _0523_ContinuousSubarraySum();
42+
boolean result = solution.checkSubarraySum(new int[]{5, 0, 0, 0}, 3);
43+
System.out.println(result);
44+
}
45+
}

0 commit comments

Comments
 (0)