Skip to content

Commit d7bd8c6

Browse files
authored
Merge pull request #1 from azl397985856/master
update
2 parents c0f7b3c + 02a3ec3 commit d7bd8c6

File tree

5 files changed

+87
-15
lines changed

5 files changed

+87
-15
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
name: Daily Problem
3+
about: Contribute Daily Problem
4+
title: "【每日一题】- 2020-xx-xx - xxx "
5+
labels: Daily Question
6+
assignees: ''
7+
8+
---
9+
10+
[anything]
11+
12+
题目地址:xxxxxx

.github/ISSUE_TEMPLATE/translation.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
name: Translation
3+
about: translation
4+
title: 'feat(translation): xxxxxxx'
5+
labels: 国际化
6+
assignees: ''
7+
8+
---
9+
10+

README.md

Lines changed: 1 addition & 0 deletions
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/102.binary-tree-level-order-traversal.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@ https://leetcode.com/problems/binary-tree-level-order-traversal/description/
44

55
## 题目描述
66
```
7-
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
7+
给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。
8+
9+
 
10+
11+
示例:
12+
二叉树:[3,9,20,null,null,15,7],
813
9-
For example:
10-
Given binary tree [3,9,20,null,null,15,7],
1114
3
1215
/ \
1316
9 20
1417
/ \
1518
15 7
16-
return its level order traversal as:
19+
返回其层次遍历结果:
20+
1721
[
1822
[3],
1923
[9,20],
@@ -23,6 +27,8 @@ return its level order traversal as:
2327

2428
## 思路
2529

30+
这是一个典型的二叉树遍历问题, 关于二叉树遍历,我总结了一个[专题](https://github.com/azl397985856/leetcode/blob/master/thinkings/binary-tree-traversal.md),大家可以先去看下那个,然后再来刷这道题。
31+
2632
这道题可以借助`队列`实现,首先把root入队,然后入队一个特殊元素Null(来表示每层的结束)。
2733

2834

@@ -223,6 +229,18 @@ class Solution:
223229
return result
224230
```
225231

232+
***复杂度分析***
233+
- 时间复杂度:$O(N)$,其中N为树中节点总数。
234+
- 空间复杂度:$O(N)$,其中N为树中节点总数。
235+
236+
更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经30K star啦。
237+
238+
大家也可以关注我的公众号《脑洞前端》获取更多更新鲜的LeetCode题解
239+
240+
## 扩展
241+
242+
实际上这道题方法很多, 比如经典的三色标记法。
243+
226244
## 相关题目
227245
- [103.binary-tree-zigzag-level-order-traversal](./103.binary-tree-zigzag-level-order-traversal.md)
228246
- [104.maximum-depth-of-binary-tree](./104.maximum-depth-of-binary-tree.md)

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

Lines changed: 42 additions & 11 deletions
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)