https://leetcode-cn.com/problems/subarray-sum-equals-k/solution/he-wei-kde-zi-shu-zu-by-leetcode-solution/
方法一:
class Solution {
public int subarraySum(int[] nums, int k) {
int count = 0, pre = 0;
HashMap <Integer, Integer> mp = new HashMap<>();
mp.put(0, 1);
for (int i = 0; i < nums.length; i++) {
pre += nums[i];
if (mp.containsKey(pre - k))
count += mp.get(pre - k);
mp.put(pre, mp.getOrDefault(pre, 0) + 1);
}
return count;
}
}
方法二:
class Solution {
public int subarraySum(int[] nums, int k) {
int count = 0;
for (int start = 0; start < nums.length; ++start) {
int sum = 0;
for (int end = start; end >= 0; --end) {
sum += nums[end];
if (sum == k) {
count++;
}
}
}
return count;
}
}
这篇博客详细介绍了如何解决LeetCode上的560题,即寻找和为K的子数组问题。作者提供了两种不同的解题方法,并可能涉及Java编程、动态规划算法以及在实现过程中遇到的bug修复。此外,还可能讨论了与MySQL相关的数据处理技巧。
1万+

被折叠的 条评论
为什么被折叠?



