Skip to content

Commit

Permalink
improved doc
Browse files Browse the repository at this point in the history
  • Loading branch information
dashidhy committed Aug 3, 2020
1 parent 16fc488 commit e87e9b7
Show file tree
Hide file tree
Showing 17 changed files with 213 additions and 165 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 说明

本项目为原项目 [algorithm-pattern](https://github.com/greyireland/algorithm-pattern) 的 Python3 语言实现版本,原项目使用 go 语言实现,目前已获 8k star。在原项目基础上,本项目添加了优先级队列,并查集,图相关算法等内容,基本覆盖了所有基础数据结构和算法,非常适合找工刷题的同学快速上手。以下为原项目 README。

# 算法模板

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

### 入门篇 🐶

- [go 语言入门](./introduction/golang.md)
- [使用 Python3 写算法题](./introduction/python.md)

- [算法快速入门](./introduction/quickstart.md)

### 数据结构篇 🐰

- [二叉树](./data_structure/binary_tree.md)
- [链表](./data_structure/linked_list.md)
- [栈和队列](./data_structure/stack_queue.md)
- [优先级队列(堆)](./data_structure/heap.md)
- [优先级队列 (堆)](./data_structure/heap.md)
- [并查集](./data_structure/union_find.md)
- [二进制](./data_structure/binary_op.md)

Expand Down
6 changes: 2 additions & 4 deletions advanced_algorithm/backtrack.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class Solution:
n = len(nums)
result = []

route = []
def backtrack(start, k):
def backtrack(start, k, route=[]):
if len(route) == k:
result.append(route.copy())
return
Expand Down Expand Up @@ -68,8 +67,7 @@ class Solution:
n = len(nums)
result = []

route = []
def backtrack(start, k):
def backtrack(start, k, route=[]):

if len(route) == k:
result.append(route.copy())
Expand Down
10 changes: 5 additions & 5 deletions advanced_algorithm/recursion.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

## 示例

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

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

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

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

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

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

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

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

> 斐波那契数,通常用  F(n) 表示,形成的序列称为斐波那契数列。该数列由  0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是:
> F(0) = 0,   F(1) = 1
Expand Down
8 changes: 4 additions & 4 deletions advanced_algorithm/slide_window.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void slidingWindow(string s, string t) {
## 示例
[minimum-window-substring](https://leetcode-cn.com/problems/minimum-window-substring/)
### [minimum-window-substring](https://leetcode-cn.com/problems/minimum-window-substring/)
> 给你一个字符串 S、一个字符串 T,请在字符串 S 里面找出:包含 T 所有字母的最小子串
Expand Down Expand Up @@ -91,7 +91,7 @@ class Solution:
return min_str
```

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

> 给定两个字符串  **s1**  和  **s2**,写一个函数来判断  **s2**  是否包含  **s1 **的排列。
Expand Down Expand Up @@ -131,7 +131,7 @@ class Solution:
return False
```

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

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

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

> 给定一个字符串,请你找出其中不含有重复字符的   最长子串   的长度。
> 示例  1:
Expand Down
13 changes: 6 additions & 7 deletions basic_algorithm/binary_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

典型示例

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

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

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

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

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

模板 2:
- 模板 2:

```Python
class Solution:
Expand Down Expand Up @@ -101,7 +101,7 @@ class Solution:

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

```Python
# 使用模板3的解法
Expand Down Expand Up @@ -142,8 +142,9 @@ class Solution:
return Range
```

- 使用模板 2 的解法

```Python
# 使用模板2的解法
class Solution:
def searchRange(self, nums, target):
Range = [-1, -1]
Expand Down Expand Up @@ -175,8 +176,6 @@ class Solution:
return Range
```



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

> 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
Expand Down
4 changes: 3 additions & 1 deletion basic_algorithm/graph/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# 图相关算法

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

### [深度优先搜索,广度优先搜索](./bfs_dfs.md)

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

Expand Down
4 changes: 2 additions & 2 deletions basic_algorithm/graph/bfs_dfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def BFS(x):

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

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

```Python
class Solution:
Expand Down
8 changes: 4 additions & 4 deletions basic_algorithm/graph/shortest_path.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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

```Python
inf = 2147483647
Expand Down Expand Up @@ -62,7 +62,7 @@ class Solution:

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

```Python
class Solution:
Expand Down Expand Up @@ -119,7 +119,7 @@ class Solution:

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

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

```Python
class Solution:
Expand Down Expand Up @@ -147,7 +147,7 @@ class Solution:

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

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

```Python
class Solution:
Expand Down
6 changes: 3 additions & 3 deletions basic_algorithm/sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ if __name__ == '__main__':

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

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

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

```Python
class Solution:
Expand All @@ -157,7 +157,7 @@ class Solution:
return min_heap[0]
```

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

```Python
class Solution:
Expand Down
16 changes: 8 additions & 8 deletions data_structure/binary_op.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ diff=(n&(n-1))^n

## 常见题目

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

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

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

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

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

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

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

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

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

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

```Python
# x <- x // 2
Expand Down Expand Up @@ -148,7 +148,7 @@ class Solution:
return num_ones
```

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

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

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

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

0 comments on commit e87e9b7

Please sign in to comment.