Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite87e9b7

Browse files
committed
improved doc
1 parent16fc488 commite87e9b7

17 files changed

+213
-165
lines changed

‎README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#说明
2+
3+
本项目为原项目[algorithm-pattern](https://github.com/greyireland/algorithm-pattern) 的 Python3 语言实现版本,原项目使用 go 语言实现,目前已获 8k star。在原项目基础上,本项目添加了优先级队列,并查集,图相关算法等内容,基本覆盖了所有基础数据结构和算法,非常适合找工刷题的同学快速上手。以下为原项目 README。
4+
15
#算法模板
26

37
算法模板,最科学的刷题方式,最快速的刷题路径,一个月从入门到 offer,你值得拥有 🐶~
@@ -16,15 +20,16 @@
1620

1721
###入门篇 🐶
1822

19-
-[go 语言入门](./introduction/golang.md)
23+
-[使用 Python3 写算法题](./introduction/python.md)
24+
2025
-[算法快速入门](./introduction/quickstart.md)
2126

2227
###数据结构篇 🐰
2328

2429
-[二叉树](./data_structure/binary_tree.md)
2530
-[链表](./data_structure/linked_list.md)
2631
-[栈和队列](./data_structure/stack_queue.md)
27-
-[优先级队列(堆)](./data_structure/heap.md)
32+
-[优先级队列(堆)](./data_structure/heap.md)
2833
-[并查集](./data_structure/union_find.md)
2934
-[二进制](./data_structure/binary_op.md)
3035

‎advanced_algorithm/backtrack.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ class Solution:
3737
n=len(nums)
3838
result= []
3939

40-
route= []
41-
defbacktrack(start,k):
40+
defbacktrack(start,k,route=[]):
4241
iflen(route)== k:
4342
result.append(route.copy())
4443
return
@@ -68,8 +67,7 @@ class Solution:
6867
n=len(nums)
6968
result= []
7069

71-
route= []
72-
defbacktrack(start,k):
70+
defbacktrack(start,k,route=[]):
7371

7472
iflen(route)== k:
7573
result.append(route.copy())

‎advanced_algorithm/recursion.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
##示例
88

9-
[reverse-string](https://leetcode-cn.com/problems/reverse-string/)
9+
###[reverse-string](https://leetcode-cn.com/problems/reverse-string/)
1010

1111
>编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组  `char[]`  的形式给出。
1212
@@ -28,7 +28,7 @@ class Solution:
2828
return
2929
```
3030

31-
[swap-nodes-in-pairs](https://leetcode-cn.com/problems/swap-nodes-in-pairs/)
31+
###[swap-nodes-in-pairs](https://leetcode-cn.com/problems/swap-nodes-in-pairs/)
3232

3333
>给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
3434
>**你不能只是单纯的改变节点内部的值**,而是需要实际的进行节点交换。
@@ -47,7 +47,7 @@ class Solution:
4747
return head
4848
```
4949

50-
[unique-binary-search-trees-ii](https://leetcode-cn.com/problems/unique-binary-search-trees-ii/)
50+
###[unique-binary-search-trees-ii](https://leetcode-cn.com/problems/unique-binary-search-trees-ii/)
5151

5252
>给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树。
5353
@@ -76,9 +76,9 @@ class Solution:
7676
return generateTrees_rec(1, n)if n>0else []
7777
```
7878

79-
##递归+备忘录
79+
##递归 +备忘录 (recursion with memorization, top-down DP)
8080

81-
[fibonacci-number](https://leetcode-cn.com/problems/fibonacci-number/)
81+
###[fibonacci-number](https://leetcode-cn.com/problems/fibonacci-number/)
8282

8383
>斐波那契数,通常用  F(n) 表示,形成的序列称为斐波那契数列。该数列由  0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是:
8484
>F(0) = 0,   F(1) = 1

‎advanced_algorithm/slide_window.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void slidingWindow(string s, string t) {
4444
4545
## 示例
4646
47-
[minimum-window-substring](https://leetcode-cn.com/problems/minimum-window-substring/)
47+
###[minimum-window-substring](https://leetcode-cn.com/problems/minimum-window-substring/)
4848
4949
> 给你一个字符串 S、一个字符串 T,请在字符串 S 里面找出:包含 T 所有字母的最小子串
5050
@@ -91,7 +91,7 @@ class Solution:
9191
return min_str
9292
```
9393

94-
[permutation-in-string](https://leetcode-cn.com/problems/permutation-in-string/)
94+
###[permutation-in-string](https://leetcode-cn.com/problems/permutation-in-string/)
9595

9696
>给定两个字符串  **s1**  和  **s2**,写一个函数来判断  **s2**  是否包含  **s1 **的排列。
9797
@@ -131,7 +131,7 @@ class Solution:
131131
returnFalse
132132
```
133133

134-
[find-all-anagrams-in-a-string](https://leetcode-cn.com/problems/find-all-anagrams-in-a-string/)
134+
###[find-all-anagrams-in-a-string](https://leetcode-cn.com/problems/find-all-anagrams-in-a-string/)
135135

136136
>给定一个字符串  ****和一个非空字符串  **p**,找到  ****中所有是  ****的字母异位词的子串,返回这些子串的起始索引。
137137
@@ -175,7 +175,7 @@ class Solution:
175175
return results
176176
```
177177

178-
[longest-substring-without-repeating-characters](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/)
178+
###[longest-substring-without-repeating-characters](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/)
179179

180180
>给定一个字符串,请你找出其中不含有重复字符的   最长子串   的长度。
181181
>示例  1:

‎basic_algorithm/binary_search.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
典型示例
1717

18-
[binary-search](https://leetcode-cn.com/problems/binary-search/)
18+
###[binary-search](https://leetcode-cn.com/problems/binary-search/)
1919

2020
>给定一个  n  个元素有序的(升序)整型数组  nums 和一个目标值  target  ,写一个函数搜索  nums  中的 target,如果目标值存在返回下标,否则返回 -1。
2121
@@ -48,7 +48,7 @@ class Solution:
4848

4949
所以用模板#3 就对了,详细的对比可以这边文章介绍:[二分搜索模板](https://leetcode-cn.com/explore/learn/card/binary-search/212/template-analysis/847/)
5050

51-
如果是最简单的二分搜索,不需要找第一个、最后一个位置、或者是没有重复元素,可以使用模板 1,代码更简洁
51+
-如果是最简单的二分搜索,不需要找第一个、最后一个位置、或者是没有重复元素,可以使用模板 1,代码更简洁
5252

5353
```Python
5454
# 无重复元素搜索时,更方便
@@ -72,7 +72,7 @@ class Solution:
7272
# 这样可以继续向子节点搜索,如:node:=node.Children[start]
7373
```
7474

75-
模板 2:
75+
-模板 2:
7676

7777
```Python
7878
classSolution:
@@ -101,7 +101,7 @@ class Solution:
101101

102102
>给定一个包含 n 个整数的排序数组,找出给定目标值 target 的起始和结束位置。如果目标值不在数组中,则返回`[-1, -1]`
103103
104-
思路:核心点就是找第一个 target 的索引,和最后一个 target 的索引,所以用两次二分搜索分别找第一次和最后一次的位置
104+
-思路:核心点就是找第一个 target 的索引,和最后一个 target 的索引,所以用两次二分搜索分别找第一次和最后一次的位置
105105

106106
```Python
107107
# 使用模板3的解法
@@ -142,8 +142,9 @@ class Solution:
142142
return Range
143143
```
144144

145+
- 使用模板 2 的解法
146+
145147
```Python
146-
# 使用模板2的解法
147148
classSolution:
148149
defsearchRange(self,nums,target):
149150
Range= [-1,-1]
@@ -175,8 +176,6 @@ class Solution:
175176
return Range
176177
```
177178

178-
179-
180179
###[search-insert-position](https://leetcode-cn.com/problems/search-insert-position/)
181180

182181
>给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。

‎basic_algorithm/graph/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#图相关算法
22

3-
###[深度优先搜索和广度优先搜索](./bfs_dfs.md)
3+
图 (graph) 是一种相当复杂的数据结构,相关算法也多种多样,以下总结一些比较基础且常见的图算法。
4+
5+
###[深度优先搜索,广度优先搜索](./bfs_dfs.md)
46

57
###[拓扑排序](./topological_sorting.md)
68

‎basic_algorithm/graph/bfs_dfs.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def BFS(x):
8282

8383
>给定一个二维矩阵,矩阵中元素 -1 表示墙或是障碍物,0 表示一扇门,INF (2147483647) 表示一个空的房间。你要给每个空房间位上填上该房间到最近门的距离,如果无法到达门,则填 INF 即可。
8484
85-
典型的多源最短路径问题,将所有源作为 BFS 的第一层即可
85+
- 思路:典型的多源最短路径问题,将所有源作为 BFS 的第一层即可
8686

8787
```Python
8888
inf=2147483647
@@ -137,7 +137,7 @@ class Solution:
137137
>在给定的 01 矩阵 A 中,存在两座岛 (岛是由四面相连的 1 形成的一个连通分量)。现在,我们可以将 0 变为 1,以使两座岛连接起来,变成一座岛。返回必须翻转的 0 的最小数目。
138138
>
139139
140-
思路:DFS 遍历连通分量找边界,从边界开始 BFS找最短路径
140+
-思路:DFS 遍历连通分量找边界,从边界开始 BFS找最短路径
141141

142142
```Python
143143
classSolution:

‎basic_algorithm/graph/shortest_path.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
>给定一个二维矩阵,矩阵中元素 -1 表示墙或是障碍物,0 表示一扇门,INF (2147483647) 表示一个空的房间。你要给每个空房间位上填上该房间到最近门的距离,如果无法到达门,则填 INF 即可。
1010
11-
典型的多源最短路径问题,将所有源作为 BFS 的第一层即可
11+
- 思路:典型的多源最短路径问题,将所有源作为 BFS 的第一层即可
1212

1313
```Python
1414
inf=2147483647
@@ -62,7 +62,7 @@ class Solution:
6262

6363
>在给定的 01 矩阵 A 中,存在两座岛 (岛是由四面相连的 1 形成的一个连通分量)。现在,我们可以将 0 变为 1,以使两座岛连接起来,变成一座岛。返回必须翻转的 0 的最小数目。
6464
65-
思路:DFS 遍历连通分量找边界,从边界开始 BFS找最短路径
65+
-思路:DFS 遍历连通分量找边界,从边界开始 BFS找最短路径
6666

6767
```Python
6868
classSolution:
@@ -119,7 +119,7 @@ class Solution:
119119

120120
###[network-delay-time](https://leetcode-cn.com/problems/network-delay-time/)
121121

122-
标准的单源最短路径问题,使用朴素的的 Dijikstra 算法即可,可以当成模板使用
122+
-标准的单源最短路径问题,使用朴素的 Dijikstra 算法即可。
123123

124124
```Python
125125
classSolution:
@@ -147,7 +147,7 @@ class Solution:
147147

148148
###[cheapest-flights-within-k-stops](https://leetcode-cn.com/problems/cheapest-flights-within-k-stops/)
149149

150-
在标准的单源最短路径问题上限制了路径的边数,因此需要同时维护当前 SPT 内每个结点最短路径的边数,当遇到边数更小的路径 (边权和可以更大) 时结点需要重新入堆,以更新后继在边数上限内没达到的结点。
150+
-在标准的单源最短路径问题上限制了路径的边数,因此需要同时维护当前 SPT 内每个结点最短路径的边数,当遇到边数更小的路径 (边权和可以更大) 时结点需要重新入堆,以更新后继在边数上限内没达到的结点。
151151

152152
```Python
153153
classSolution:

‎basic_algorithm/sort.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ if __name__ == '__main__':
137137

138138
###[kth-largest-element-in-an-array](https://leetcode-cn.com/problems/kth-largest-element-in-an-array/)
139139

140-
思路 1: sort 后取第 k 个,最简单直接,复杂度 O(N log N) 代码略
140+
-思路 1: sort 后取第 k 个,最简单直接,复杂度 O(N log N) 代码略
141141

142-
思路 2: 使用最小堆,复杂度 O(N log k)
142+
-思路 2: 使用最小堆,复杂度 O(N log k)
143143

144144
```Python
145145
classSolution:
@@ -157,7 +157,7 @@ class Solution:
157157
return min_heap[0]
158158
```
159159

160-
思路 3: Quick select,方式类似于快排,每次 partition 后检查 pivot 是否为第 k 个元素,如果是则直接返回,如果比 k 大,则继续 partition 小于 pivot 的元素,如果比 k 小则继续 partition 大于 pivot 的元素。相较于快排,quick select 每次只需 partition 一侧,因此平均复杂度为 O(N)
160+
-思路 3:[Quick select](https://en.wikipedia.org/wiki/Quickselect),方式类似于快排,每次 partition 后检查 pivot 是否为第 k 个元素,如果是则直接返回,如果比 k 大,则继续 partition 小于 pivot 的元素,如果比 k 小则继续 partition 大于 pivot 的元素。相较于快排,quick select 每次只需 partition 一侧,因此平均复杂度为 O(N)
161161

162162
```Python
163163
classSolution:

‎data_structure/binary_op.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ diff=(n&(n-1))^n
2828

2929
##常见题目
3030

31-
[single-number](https://leetcode-cn.com/problems/single-number/)
31+
###[single-number](https://leetcode-cn.com/problems/single-number/)
3232

3333
>给定一个**非空**整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
3434
@@ -43,7 +43,7 @@ class Solution:
4343
return out
4444
```
4545

46-
[single-number-ii](https://leetcode-cn.com/problems/single-number-ii/)
46+
###[single-number-ii](https://leetcode-cn.com/problems/single-number-ii/)
4747

4848
>给定一个**非空**整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次。找出那个只出现了一次的元素。
4949
@@ -59,7 +59,7 @@ class Solution:
5959
return seen_once
6060
```
6161

62-
[single-number-iii](https://leetcode-cn.com/problems/single-number-iii/)
62+
###[single-number-iii](https://leetcode-cn.com/problems/single-number-iii/)
6363

6464
>给定一个整数数组  `nums`,其中恰好有两个元素只出现一次,其余所有元素均出现两次。 找出只出现一次的那两个元素。
6565
@@ -83,7 +83,7 @@ class Solution:
8383
return [x, bitmask^x]
8484
```
8585

86-
[number-of-1-bits](https://leetcode-cn.com/problems/number-of-1-bits/)
86+
###[number-of-1-bits](https://leetcode-cn.com/problems/number-of-1-bits/)
8787

8888
>编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’  的个数(也被称为[汉明重量](https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E9%87%8D%E9%87%8F))。
8989
@@ -97,11 +97,11 @@ class Solution:
9797
return num_ones
9898
```
9999

100-
[counting-bits](https://leetcode-cn.com/problems/counting-bits/)
100+
###[counting-bits](https://leetcode-cn.com/problems/counting-bits/)
101101

102102
>给定一个非负整数  **num**。对于  0 ≤ i ≤ num  范围中的每个数字  i ,计算其二进制数中的 1 的数目并将它们作为数组返回。
103103
104-
思路:利用上一题的解法容易想到 O(nk) 的解法,k 为位数。但是实际上可以利用动态规划将复杂度降到 O(n),想法其实也很简单,即当前数的 1 个数等于比它少一个 1 的数的结果加 1。下面给出三种 DP 解法
104+
-思路:利用上一题的解法容易想到 O(nk) 的解法,k 为位数。但是实际上可以利用动态规划将复杂度降到 O(n),想法其实也很简单,即当前数的 1 个数等于比它少一个 1 的数的结果加 1。下面给出三种 DP 解法
105105

106106
```Python
107107
# x <- x // 2
@@ -148,7 +148,7 @@ class Solution:
148148
return num_ones
149149
```
150150

151-
[reverse-bits](https://leetcode-cn.com/problems/reverse-bits/)
151+
###[reverse-bits](https://leetcode-cn.com/problems/reverse-bits/)
152152

153153
>颠倒给定的 32 位无符号整数的二进制位。
154154
@@ -172,7 +172,7 @@ class Solution:
172172
return (byte*0x0202020202&0x010884422010)%1023
173173
```
174174

175-
[bitwise-and-of-numbers-range](https://leetcode-cn.com/problems/bitwise-and-of-numbers-range/)
175+
###[bitwise-and-of-numbers-range](https://leetcode-cn.com/problems/bitwise-and-of-numbers-range/)
176176

177177
>给定范围[m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点)。
178178

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp