Skip to content

Commit

Permalink
再次完成560
Browse files Browse the repository at this point in the history
  • Loading branch information
diguage committed Jun 23, 2024
1 parent 6791f99 commit eeb6e05
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3928,7 +3928,7 @@
|{source_base_url}/_0560_SubarraySumEqualsK.java[Java]
|{doc_base_url}/0560-subarray-sum-equals-k.adoc[Note]
|Medium
|
|前缀和

//|561
//|{leetcode_base_url}/array-partition-i/[Array Partition I]
Expand Down
27 changes: 25 additions & 2 deletions docs/0560-subarray-sum-equals-k.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,40 @@ Given an array of integers and an integer **k**, you need to find the total numb

在完成遍历数组后,`count` 记录了所需结果

基于一个idea:`sum[j] - sum[i] == k` 的话,`nums[i+1, j]` 之间数字的和就是 `k`。

image::images/0560-01.png[]

image::images/0560-02.png[]

image::images/0560-03.png[]

image::images/0560-04.png[]

image::images/0560-05.png[]

image::images/0560-06.png[]

image::images/0560-07.png[]

image::images/0560-08.png[]

image::images/0560-09.png[]

[[src-0560]]
[{java_src_attr}]
----
include::{sourcedir}/_0560_SubarraySumEqualsK.java[]
----

这个方法是基于一个idea:`sum[j] - sum[i] == k` 的话,`nums[i, j ]` 之间数字的和就是 `k`。
[{java_src_attr}]
----
include::{sourcedir}/_0560_SubarraySumEqualsK_2.java[]
----


== 参考资料

. https://leetcode-cn.com/problems/subarray-sum-equals-k/solution/he-wei-kde-zi-shu-zu-by-leetcode/[和为K的子数组 - 和为K的子数组 - 力扣(LeetCode)]
. https://leetcode.cn/problems/continuous-subarray-sum/solutions/807930/lian-xu-de-zi-shu-zu-he-by-leetcode-solu-rdzi/[和为K的子数组 - 和为K的子数组 - 官方题解^]


Binary file added docs/images/0560-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0560-02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0560-03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0560-04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0560-05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0560-06.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0560-07.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0560-08.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0560-09.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* https://leetcode.com/problems/subarray-sum-equals-k/[Subarray Sum Equals K - LeetCode]
*
* @author D瓜哥, https://www.diguage.com/
* @author D瓜哥 · https://www.diguage.com
* @since 2020-01-30 23:14
*/
public class _0560_SubarraySumEqualsK {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.diguage.algorithm.leetcode;

import java.util.*;

/**
* = 560. Subarray Sum Equals K
*
* https://leetcode.com/problems/subarray-sum-equals-k/[Subarray Sum Equals K - LeetCode]
*
* @author D瓜哥 · https://www.diguage.com
* @since 2024-06-23 11:08:29
*/
public class _0560_SubarraySumEqualsK_2 {
/**
* 自己根据“前缀和”套路想的思路,参考 https://leetcode.cn/problems/subarray-sum-equals-k/solutions/238572/he-wei-kde-zi-shu-zu-by-leetcode-solution/[560. 和为 K 的子数组 - 官方题解^] 更正了代码。
*/
public int subarraySum(int[] nums, int k) {
int result = 0;
if (nums == null || nums.length == 0) {
return result;
}
// key:前缀和,value:key 对应的前缀和的个数
Map<Integer, Integer> sumToCntMap = new HashMap<>();
// 对于下标为 0 的元素,前缀和为 0,个数为 1
sumToCntMap.put(0, 1);
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
// 先获得前缀和为 preSum - k 的个数,加到计数变量里
// TODO 这里为什么先检查是否存在?
if (sumToCntMap.containsKey(sum - k)) {
result += sumToCntMap.get(sum - k);
}
// 然后维护 preSumFreq 的定义
sumToCntMap.put(sum, sumToCntMap.getOrDefault(sum, 0) + 1);
}
return result;
}

public static void main(String[] args) {
_0560_SubarraySumEqualsK_2 solution = new _0560_SubarraySumEqualsK_2();

int[] n4 = {3, 4, 7, 2, -3, 1, 4, 2};
int r4 = solution.subarraySum(n4, 7);
System.out.println((r4 == 4) + " : " + r4);

int[] n3 = {1};
int r3 = solution.subarraySum(n3, 0);
System.out.println((r3 == 0) + " : " + r3);

int[] n2 = {-1, -1, 1};
int r2 = solution.subarraySum(n2, 0);
System.out.println((r2 == 1) + " : " + r2);

int[] n1 = {1, 1, 1};
int r1 = solution.subarraySum(n1, 2);
System.out.println((r1 == 2) + " : " + r1);
}
}

0 comments on commit eeb6e05

Please sign in to comment.