Skip to content

Commit b1a9ea3

Browse files
author
lucifer
committed
feat: 560
1 parent c79b7a3 commit b1a9ea3

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
239239
- [0516.longest-palindromic-subsequence](./problems/516.longest-palindromic-subsequence.md)
240240
- [0518.coin-change-2](./problems/518.coin-change-2.md)
241241
- [0547.friend-circles](./problems/547.friend-circles-en.md) 🆕
242+
- [0560.subarray-sum-equals-k](./problems/560.subarray-sum-equals-k.md)
242243
- [0609.find-duplicate-file-in-system](./problems/609.find-duplicate-file-in-system.md)
243244
- [0820.short-encoding-of-words](./problems/820.short-encoding-of-words.md) 🆕
244245
- [0875.koko-eating-bananas](./problems/875.koko-eating-bananas.md)

problems/560.subarray-sum-equals-k.md

+42-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
## 题目地址
2+
23
https://leetcode.com/problems/subarray-sum-equals-k/description/
34

45
## 题目描述
6+
57
```
68
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.
79
@@ -13,31 +15,60 @@ The length of the array is in range [1, 20,000].
1315
The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].
1416
1517
```
18+
1619
## 思路
17-
符合直觉的做法是暴力求解所有的子数组,然后分别计算和,如果等于k,count就+1.这种做法的时间复杂度为O(n^2).
1820

19-
这里有一种更加巧妙的方法,我们可以借助额外的空间,使用hashmap来简化时间复杂度,这种算法的时间复杂度可以达到O(n).
21+
符合直觉的做法是暴力求解所有的子数组,然后分别计算和,如果等于 k,count 就+1.这种做法的时间复杂度为 O(n^2),代码如下:
22+
23+
```python
24+
class Solution:
25+
def subarraySum(self, nums: List[int], k: int) -> int:
26+
cnt, n = 0, len(nums)
27+
for i in range(n):
28+
for j in range(i, n):
29+
if (sum(nums[i:j + 1]) == k): cnt += 1
30+
return cnt
31+
```
32+
33+
实际上刚开始看到这题目的时候,我想“是否可以用滑动窗口解决?”。但是很快我就放弃了,因为看了下数组中项的取值范围有负数,这样我们扩张或者收缩窗口就比较复杂。第二个想法是前缀和,保存一个数组的前缀和,然后利用差分法得出任意区间段的和,这种想法是可行的,代码如下:
34+
35+
```python
36+
class Solution:
37+
def subarraySum(self, nums: List[int], k: int) -> int:
38+
cnt, n = 0, len(nums)
39+
pre = [0] * (n + 1)
40+
for i in range(1, n + 1):
41+
pre[i] = pre[i - 1] + nums[i - 1]
42+
for i in range(1, n + 1):
43+
for j in range(i, n + 1):
44+
if (pre[j] - pre[i - 1] == k): cnt += 1
45+
return cnt
46+
```
47+
48+
这里有一种更加巧妙的方法,可以不使用前缀和数组,而是使用 hashmap 来简化时间复杂度,这种算法的时间复杂度可以达到 O(n).
49+
50+
具体算法:
2051

21-
我们维护一个hashmap,hashmap的key为累加值acc,value为累加值acc出现的次数。
22-
我们迭代数组,然后不断更新acc和hashmap,如果acc 等于k,那么很明显应该+1. 如果hashmap[acc - k] 存在,
23-
我们就把它加到结果中去即可。
52+
- 维护一个 hashmap,hashmap 的 key 为累加值 acc,value 为累加值 acc 出现的次数。
53+
- 迭代数组,然后不断更新 acc 和 hashmap,如果 acc 等于 k,那么很明显应该+1. 如果 hashmap[acc - k] 存在,我们就把它加到结果中去即可。
2454

25-
语言比较难以解释,我画了一个图来演示nums = [1,2,3,3,0,3,4,2], k = 6的情况
55+
语言比较难以解释,我画了一个图来演示 nums = [1,2,3,3,0,3,4,2], k = 6 的情况
2656

2757
![560.subarray-sum-equals-k](../assets/problems/560.subarray-sum-equals-k.jpg)
2858

29-
如图,当访问到nums[3]的时候,hashmap如图所示,这个时候count为2.
59+
如图,当访问到 nums[3]的时候,hashmap 如图所示,这个时候 count 为 2.
3060
其中之一是[1,2,3],这个好理解。还有一个是[3,3].
3161

32-
这个[3,3]正是我们通过hashmap[acc - k]即hashmap[9 - 6]得到的。
62+
这个[3,3]正是我们通过 hashmap[acc - k]即 hashmap[9 - 6]得到的。
3363

3464
## 关键点解析
3565

36-
- 可以利用hashmap记录和的累加值来避免重复计算
66+
- 前缀和
67+
- 可以利用 hashmap 记录和的累加值来避免重复计算
3768

3869
## 代码
3970

40-
* 语言支持:JS, Python
71+
- 语言支持:JS, Python
4172

4273
Javascript Code:
4374

@@ -52,7 +83,7 @@ Javascript Code:
5283
* @param {number} k
5384
* @return {number}
5485
*/
55-
var subarraySum = function(nums, k) {
86+
var subarraySum = function (nums, k) {
5687
const hashmap = {};
5788
let acc = 0;
5889
let count = 0;

0 commit comments

Comments
 (0)