Skip to content

Commit f4d8cd3

Browse files
author
lucifer
committed
feat: 添加前置知识
1 parent 1368901 commit f4d8cd3

28 files changed

+701
-518
lines changed

problems/21.MergeTwoSortedLists.md

+25-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
## 题目地址
2+
23
https://leetcode-cn.com/problems/merge-two-sorted-lists
34

45
## 题目描述
6+
57
```
68
将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 
79
@@ -12,17 +14,23 @@ https://leetcode-cn.com/problems/merge-two-sorted-lists
1214
1315
```
1416

17+
## 前置知识
18+
19+
- 递归
20+
- 链表
21+
1522
## 思路
16-
使用递归来解题,将两个链表头部较小的一个与剩下的元素合并,并返回排好序的链表头,当两条链表中的一条为空时终止递归。
1723

24+
使用递归来解题,将两个链表头部较小的一个与剩下的元素合并,并返回排好序的链表头,当两条链表中的一条为空时终止递归。
1825

1926
## 关键点
2027

2128
- 掌握链表数据结构
22-
- 考虑边界情况
29+
- 考虑边界情况 2
2330

2431
## 代码
25-
* 语言支持:JS
32+
33+
- 语言支持:JS
2634

2735
```js
2836
/**
@@ -38,26 +46,25 @@ https://leetcode-cn.com/problems/merge-two-sorted-lists
3846
* @return {ListNode}
3947
*/
4048
const mergeTwoLists = function (l1, l2) {
41-
if (l1 === null) {
42-
return l2;
43-
}
44-
if (l2 === null) {
45-
return l1;
46-
}
47-
if (l1.val < l2.val) {
48-
l1.next = mergeTwoLists(l1.next, l2);
49-
return l1;
50-
} else {
51-
l2.next = mergeTwoLists(l1, l2.next);
52-
return l2;
53-
}
49+
if (l1 === null) {
50+
return l2;
51+
}
52+
if (l2 === null) {
53+
return l1;
54+
}
55+
if (l1.val < l2.val) {
56+
l1.next = mergeTwoLists(l1.next, l2);
57+
return l1;
58+
} else {
59+
l2.next = mergeTwoLists(l1, l2.next);
60+
return l2;
61+
}
5462
};
5563
```
5664

57-
***复杂度分析***
65+
**_复杂度分析_**
5866

5967
M、N 是两条链表 l1、l2 的长度
6068

6169
- 时间复杂度:O(M+N)
6270
- 空间复杂度:O(M+N)
63-

problems/22.GenerateParentheses.md

+29-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
## 题目地址
2+
23
https://leetcode-cn.com/problems/generate-parentheses
34

45
## 题目描述
6+
57
```
68
数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。
79
@@ -18,18 +20,21 @@ https://leetcode-cn.com/problems/generate-parentheses
1820
1921
```
2022

23+
## 前置知识
24+
25+
- DFS
26+
2127
## 思路
2228

2329
深度优先搜索(回溯思想),从空字符串开始构造,做加法。
2430

25-
2631
## 关键点
2732

2833
- 当 l < r 时记得剪枝
2934

30-
3135
## 代码
32-
* 语言支持:JS
36+
37+
- 语言支持:JS
3338

3439
```js
3540
/**
@@ -41,32 +46,31 @@ https://leetcode-cn.com/problems/generate-parentheses
4146
* @param res 结果集
4247
*/
4348
const generateParenthesis = function (n) {
44-
const res = [];
45-
46-
function dfs(l, r, str) {
47-
if (l == n && r == n) {
48-
return res.push(str);
49-
}
50-
// l 小于 r 时不满足条件 剪枝
51-
if (l < r) {
52-
return;
53-
}
54-
// l 小于 n 时可以插入左括号,最多可以插入 n 个
55-
if (l < n) {
56-
dfs(l + 1, r, str + '(');
57-
}
58-
// r < l 时 可以插入右括号
59-
if (r < l) {
60-
dfs(l, r + 1, str + ')');
61-
}
49+
const res = [];
50+
51+
function dfs(l, r, str) {
52+
if (l == n && r == n) {
53+
return res.push(str);
54+
}
55+
// l 小于 r 时不满足条件 剪枝
56+
if (l < r) {
57+
return;
6258
}
63-
dfs(0, 0, '');
64-
return res;
59+
// l 小于 n 时可以插入左括号,最多可以插入 n 个
60+
if (l < n) {
61+
dfs(l + 1, r, str + "(");
62+
}
63+
// r < l 时 可以插入右括号
64+
if (r < l) {
65+
dfs(l, r + 1, str + ")");
66+
}
67+
}
68+
dfs(0, 0, "");
69+
return res;
6570
};
6671
```
6772

68-
69-
***复杂度分析***
73+
**_复杂度分析_**
7074

7175
- 时间复杂度:O(2^N)
7276
- 空间复杂度:O(2^N)

problems/30.substring-with-concatenation-of-all-words.md

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/descr
3030
3131
```
3232

33+
## 前置知识
34+
35+
- 哈希表
36+
3337
## 思路
3438

3539
本题是要我们找出 words 中`所有单词按照任意顺序串联`形成的单词中恰好出现在 s 中的索引,因此顺序是不重要的。换句话说,我们只要统计每一个单词的出现情况即可。以题目中 s = "barfoothefoobarman", words = ["foo","bar"] 为例。 我们只需要统计 foo 出现了一次,bar 出现了一次即可。我们只需要在 s 中找到同样包含一次 foo 和一次 bar 的子串即可。由于 words 中的字符串都是等长的,因此编码上也会比较简单。

problems/46.permutations.md

+22-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
## 题目地址
2+
23
https://leetcode.com/problems/permutations/description/
34

45
## 题目描述
6+
57
```
68
Given a collection of distinct integers, return all possible permutations.
79
@@ -20,13 +22,17 @@ Output:
2022
2123
```
2224

25+
## 前置知识
26+
27+
- 回溯法
28+
2329
## 思路
2430

2531
这道题目是求集合,并不是`求极值`,因此动态规划不是特别切合,因此我们需要考虑别的方法。
2632

2733
这种题目其实有一个通用的解法,就是回溯法。
2834
网上也有大神给出了这种回溯法解题的
29-
[通用写法](https://leetcode.com/problems/combination-sum/discuss/16502/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partitioning)),这里的所有的解法使用通用方法解答。
35+
[通用写法](<https://leetcode.com/problems/combination-sum/discuss/16502/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partitioning)>),这里的所有的解法使用通用方法解答。
3036
除了这道题目还有很多其他题目可以用这种通用解法,具体的题目见后方相关题目部分。
3137

3238
我们先来看下通用解法的解题思路,我画了一张图:
@@ -42,7 +48,7 @@ Output:
4248

4349
## 代码
4450

45-
* 语言支持: Javascript, Python3
51+
- 语言支持: Javascript, Python3
4652

4753
Javascript Code:
4854

@@ -79,31 +85,33 @@ Javascript Code:
7985
*
8086
*/
8187
function backtrack(list, tempList, nums) {
82-
if (tempList.length === nums.length) return list.push([...tempList]);
83-
for(let i = 0; i < nums.length; i++) {
84-
if (tempList.includes(nums[i])) continue;
85-
tempList.push(nums[i]);
86-
backtrack(list, tempList, nums);
87-
tempList.pop();
88-
}
88+
if (tempList.length === nums.length) return list.push([...tempList]);
89+
for (let i = 0; i < nums.length; i++) {
90+
if (tempList.includes(nums[i])) continue;
91+
tempList.push(nums[i]);
92+
backtrack(list, tempList, nums);
93+
tempList.pop();
94+
}
8995
}
9096
/**
9197
* @param {number[]} nums
9298
* @return {number[][]}
9399
*/
94-
var permute = function(nums) {
95-
const list = [];
96-
backtrack(list, [], nums)
97-
return list
100+
var permute = function (nums) {
101+
const list = [];
102+
backtrack(list, [], nums);
103+
return list;
98104
};
99105
```
106+
100107
Python3 Code:
108+
101109
```Python
102110
class Solution:
103111
def permute(self, nums: List[int]) -> List[List[int]]:
104112
"""itertools库内置了这个函数"""
105113
return itertools.permutations(nums)
106-
114+
107115
def permute2(self, nums: List[int]) -> List[List[int]]:
108116
"""自己写回溯法"""
109117
res = []

problems/47.permutations-ii.md

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
## 题目地址
2+
23
https://leetcode.com/problems/permutations-ii/description/
34

45
## 题目描述
6+
57
```
68
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
79
@@ -17,13 +19,17 @@ Output:
1719
1820
```
1921

22+
## 前置知识
23+
24+
- 回溯法
25+
2026
## 思路
2127

2228
这道题目是求集合,并不是`求极值`,因此动态规划不是特别切合,因此我们需要考虑别的方法。
2329

2430
这种题目其实有一个通用的解法,就是回溯法。
2531
网上也有大神给出了这种回溯法解题的
26-
[通用写法](https://leetcode.com/problems/combination-sum/discuss/16502/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partitioning)),这里的所有的解法使用通用方法解答。
32+
[通用写法](<https://leetcode.com/problems/combination-sum/discuss/16502/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partitioning)>),这里的所有的解法使用通用方法解答。
2733
除了这道题目还有很多其他题目可以用这种通用解法,具体的题目见后方相关题目部分。
2834

2935
我们先来看下通用解法的解题思路,我画了一张图:
@@ -37,10 +43,9 @@ Output:
3743
- 回溯法
3844
- backtrack 解题公式
3945

40-
4146
## 代码
4247

43-
* 语言支持: Javascript,Python3
48+
- 语言支持: Javascript,Python3
4449

4550
```js
4651
/*
@@ -92,19 +97,26 @@ function backtrack(list, nums, tempList, visited) {
9297
* @param {number[]} nums
9398
* @return {number[][]}
9499
*/
95-
var permuteUnique = function(nums) {
100+
var permuteUnique = function (nums) {
96101
const list = [];
97-
backtrack(list, nums.sort((a, b) => a - b), [], []);
102+
backtrack(
103+
list,
104+
nums.sort((a, b) => a - b),
105+
[],
106+
[]
107+
);
98108
return list;
99109
};
100110
```
111+
101112
Python3 code:
113+
102114
```Python
103115
class Solution:
104116
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
105117
"""与46题一样,当然也可以直接调用itertools的函数,然后去重"""
106118
return list(set(itertools.permutations(nums)))
107-
119+
108120
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
109121
"""自己写回溯法,与46题相比,需要去重"""
110122
# 排序是为了去重

0 commit comments

Comments
 (0)