Skip to content

Commit c247ba0

Browse files
committed
algorithm-pattern-swift
0 parents  commit c247ba0

File tree

56 files changed

+9481
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+9481
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

README.md

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# 算法模板
2+
3+
![来刷题了](https://img.fuiboom.com/img/title.png)
4+
5+
算法模板,最科学的刷题方式,最快速的刷题路径,一个月从入门到 offer,你值得拥有 🐶~
6+
7+
算法模板顾名思义就是刷题的套路模板,掌握了刷题模板之后,刷题也变得好玩起来了~
8+
9+
> 此项目是自己找工作时,从 0 开始刷 LeetCode 的心得记录,通过各种刷题文章、专栏、视频等总结了一套自己的刷题模板。
10+
>
11+
> 这个模板主要是介绍了一些通用的刷题模板,以及一些常见问题,如到底要刷多少题,按什么顺序来刷题,如何提高刷题效率等。
12+
13+
## 在线文档
14+
15+
在线文档 Gitbook:[算法模板 🔥](https://greyireland.gitbook.io/algorithm-pattern/)
16+
17+
## 核心内容
18+
19+
### 入门篇 🐶
20+
21+
- [swift 语言入门](./introduction/swift.md)
22+
- [算法快速入门](./introduction/quickstart.md)
23+
24+
### 数据结构篇 🐰
25+
26+
- [二叉树](./data_structure/binary_tree.md)
27+
- [链表](./data_structure/linked_list.md)
28+
- [栈和队列](./data_structure/stack_queue.md)
29+
- [二进制](./data_structure/binary_op.md)
30+
31+
### 基础算法篇 🐮
32+
33+
- [二分搜索](./basic_algorithm/binary_search.md)
34+
- [排序算法](./basic_algorithm/sort.md)
35+
- [动态规划](./basic_algorithm/dp.md)
36+
37+
### 算法思维 🦁
38+
39+
- [递归思维](./advanced_algorithm/recursion.md)
40+
- [滑动窗口思想](./advanced_algorithm/slide_window.md)
41+
- [二叉搜索树](./advanced_algorithm/binary_search_tree.md)
42+
- [回溯法](./advanced_algorithm/backtrack.md)
43+
44+
## 心得体会
45+
46+
文章大部分是对题目的思路介绍,和一些问题的解析,有了思路还是需要自己手动写写的,所以每篇文章最后都有对应的练习题
47+
48+
刷完这些练习题,基本对数据结构和算法有自己的认识体会,基本大部分面试题都能写得出来,国内的 BAT、TMD 应该都不是问题
49+
50+
从 4 月份找工作开始,从 0 开始刷 LeetCode,中间大概花了一个半月(6 周)左右时间刷完 240 题。
51+
52+
![一个半月刷完240题](https://img.fuiboom.com/img/leetcode_time.png)
53+
54+
![刷题记录](https://img.fuiboom.com/img/leetcode_record.png)
55+
56+
开始刷题时,确实是无从下手,因为从序号开始刷,刷到几道题就遇到 hard 的题型,会卡住很久,后面去评论区看别人怎么刷题,也去 Google 搜索最好的刷题方式,发现按题型刷题会舒服很多,基本一个类型的题目,一天能做很多,慢慢刷题也不再枯燥,做起来也很有意思,最后也收到不错的 offer(最后去了宇宙系)。
57+
58+
回到最开始的问题,面试到底要刷多少题,其实这个取决于你想进什么样公司,你定的目标如果是国内一线大厂,个人感觉大概 200 至 300 题基本就满足大部分面试需要了。第二个问题是按什么顺序刷及如何提高效率,这个也是本 repo 的目的,给你指定了一个刷题的顺序,以及刷题的模板,有了方向和技巧后,就去动手吧~ 希望刷完之后,你也能自己总结一套属于自己的刷题模板,有所收获,有所成长~
59+
60+
## 推荐的刷题路径
61+
62+
按此 repo 目录刷一遍,如果中间有题目卡住了先跳过,然后刷题一遍 LeetCode 探索基础卡片,最后快要面试时刷题一遍剑指 offer。
63+
64+
为什么这么要这么刷,因为 repo 里面的题目是按类型归类,都是一些常见的高频题,很有代表性,大部分都是可以用模板加一点变形做出来,刷完后对大部分题目有基本的认识。然后刷一遍探索卡片,巩固一下一些基础知识点,总结这些知识点。最后剑指 offer 是大部分公司的出题源头,刷完面试中基本会遇到现题或者变形题,基本刷完这三部分,大部分国内公司的面试题应该就没什么问题了~
65+
66+
1、 [algorithm-pattern 练习题](https://greyireland.gitbook.io/algorithm-pattern/)
67+
68+
![练习题](https://img.fuiboom.com/img/repo_practice.png)
69+
70+
2、 [LeetCode 卡片](https://leetcode-cn.com/explore/)
71+
72+
![探索卡片](https://img.fuiboom.com/img/leetcode_explore.png)
73+
74+
3、 [剑指 offer](https://leetcode-cn.com/problemset/lcof/)
75+
76+
![剑指offer](https://img.fuiboom.com/img/leetcode_jzoffer.png)
77+
78+
刷题时间可以合理分配,如果打算准备面试了,建议前面两部分 一个半月 (6 周)时间刷完,最后剑指 offer 半个月刷完,边刷可以边投简历进行面试,遇到不会的不用着急,往模板上套就对了,如果面试管给你提示,那就好好做,不要错过这大好机会~
79+
80+
> 注意点:如果为了找工作刷题,遇到 hard 的题如果有思路就做,没思路先跳过,先把基础打好,再来刷 hard 可能效果会更好~
81+
82+
## 面试资源
83+
84+
分享一些计算机的经典书籍,大部分对面试应该都有帮助,强烈推荐 🌝
85+
86+
[我看过的 100 本书](https://github.com/greyireland/awesome-programming-books-1)
87+
88+
## 更新计划
89+
90+
持续更新中,觉得还可以的话点个 **star** 收藏呀 ⭐️~
91+
92+
【 Github 】[https://github.com/greyireland/algorithm-pattern](https://github.com/greyireland/algorithm-pattern) ⭐️
93+
94+
## 完成打卡
95+
96+
完成计划之后,可以提交 Pull requests,在下面添加自己的项目仓库,完成自己的算法模板打卡呀~
97+
98+
| 完成 | 用户 | 项目地址 |
99+
| ---- | ------------------------------------------------- | ------------------------------------------------------------------- |
100+
|| [wardseptember](https://github.com/wardseptember) | [notes(Java 实现)](https://github.com/wardseptember/notes) |
101+
|| [dashidhy](https://github.com/dashidhy) | [algorithm-pattern-python(Python 实现)](https://github.com/dashidhy/algorithm-pattern-python) |

SUMMARY.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 算法模板
2+
3+
## 入门篇
4+
5+
- [swift 语言入门](introduction/swift.md)
6+
- [算法快速入门](introduction/quickstart.md)
7+
8+
## 数据结构篇
9+
10+
- [二叉树](data_structure/binary_tree.md)
11+
- [链表](data_structure/linked_list.md)
12+
- [栈和队列](data_structure/stack_queue.md)
13+
- [二进制](data_structure/binary_op.md)
14+
15+
## 基础算法篇
16+
17+
- [二分搜索](basic_algorithm/binary_search.md)
18+
- [排序算法](basic_algorithm/sort.md)
19+
- [动态规划](basic_algorithm/dp.md)
20+
21+
## 算法思维
22+
23+
- [递归思维](advanced_algorithm/recursion.md)
24+
- [滑动窗口思想](advanced_algorithm/slide_window.md)
25+
- [二叉搜索树](advanced_algorithm/binary_search_tree.md)
26+
- [回溯法](advanced_algorithm/backtrack.md)

TODO.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# 计划
2+
3+
## v1
4+
5+
- [ ] 完善文档细节
6+
- [ ] 工程实现用到的算法解析
7+
- [ ] 周赛计划
8+
- [ ] 面试体系计划

advanced_algorithm/backtrack.md

+205
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
# 回溯法
2+
3+
## 背景
4+
5+
回溯法(backtrack)常用于遍历列表所有子集,是 DFS 深度搜索一种,一般用于全排列,穷尽所有可能,遍历的过程实际上是一个决策树的遍历过程。时间复杂度一般 O(N!),它不像动态规划存在重叠子问题可以优化,回溯算法就是纯暴力穷举,复杂度一般都很高。
6+
7+
## 模板
8+
9+
```swift
10+
result = []
11+
func backtrack(选择列表,路径):
12+
if 满足结束条件:
13+
result.add(路径)
14+
return
15+
for 选择 in 选择列表:
16+
做选择
17+
backtrack(选择列表,路径)
18+
撤销选择
19+
```
20+
21+
核心就是从选择列表里做一个选择,然后一直递归往下搜索答案,如果遇到路径不通,就返回来撤销这次选择
22+
23+
## 示例
24+
25+
### [subsets](https://leetcode-cn.com/problems/subsets/)
26+
27+
> 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)
28+
29+
遍历过程
30+
31+
![image.png](https://img.fuiboom.com/img/backtrack.png)
32+
33+
```swift
34+
//回溯
35+
//https://leetcode-cn.com/problems/subsets/solution/swift-zi-ji-hui-su-by-hu-cheng-he-da-bai-sha/
36+
//执行用时:8 ms, 在所有 Swift 提交中击败了97.96%的用户
37+
//内存消耗:13.5 MB, 在所有 Swift 提交中击败了93.90%的用户
38+
func subsets(_ nums: [Int]) -> [[Int]] {
39+
var res = [[Int]]()
40+
41+
func backtrack(start :Int, track: inout [Int]){
42+
res.append(track)
43+
for index in start..<nums.count {
44+
track.append(nums[index])
45+
backtrack(start: index+1, track: &track)
46+
track.popLast()
47+
}
48+
}
49+
50+
var items = [Int]()
51+
backtrack(start: 0, track: &items)
52+
53+
return res
54+
}
55+
56+
//迭代
57+
//https://leetcode-cn.com/problems/subsets/solution/swift-zi-ji-die-dai-by-hu-cheng-he-da-bai-sha/
58+
//执行用时:12 ms, 在所有 Swift 提交中击败了76.35%的用户
59+
//内存消耗:13.5 MB, 在所有 Swift 提交中击败了93.90%的用户
60+
func subsets_a(_ nums: [Int]) -> [[Int]] {
61+
var res = [[Int]]()
62+
res.append([Int]())
63+
64+
for num in nums {
65+
var list = [[Int]]()
66+
for item in res {
67+
list.append(item + [num])
68+
}
69+
res.append(contentsOf: list)
70+
}
71+
72+
return res
73+
}
74+
```
75+
76+
### [subsets-ii](https://leetcode-cn.com/problems/subsets-ii/)
77+
78+
> 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)说明:解集不能包含重复的子集
79+
80+
```swift
81+
//https://leetcode-cn.com/problems/subsets-ii/solution/swift-90-zi-ji-iihui-su-by-hu-cheng-he-da-bai-sha/
82+
//执行用时:12 ms, 在所有 Swift 提交中击败了100.00%的用户
83+
//内存消耗:14 MB, 在所有 Swift 提交中击败了87.50%的用户
84+
func subsetsWithDup(_ nums: [Int]) -> [[Int]] {
85+
var res = [[Int]]()
86+
let sortedNums = nums.sorted()
87+
88+
func backtrack(start :Int, track: inout [Int]){
89+
res.append(track)
90+
for index in start..<sortedNums.count {
91+
if index > start && sortedNums[index] == sortedNums[index - 1]{
92+
continue
93+
}
94+
track.append(sortedNums[index])
95+
backtrack(start: index+1, track: &track)
96+
track.popLast()
97+
}
98+
}
99+
100+
var items = [Int]()
101+
backtrack(start: 0, track: &items)
102+
103+
return res
104+
}
105+
```
106+
107+
### [permutations](https://leetcode-cn.com/problems/permutations/)
108+
109+
> 给定一个   没有重复   数字的序列,返回其所有可能的全排列
110+
111+
思路:需要记录已经选择过的元素,满足条件的结果才进行返回
112+
113+
```swift
114+
//https://leetcode-cn.com/problems/permutations/solution/swift-46-quan-pai-lie-hui-su-tong-shi-ji-lu-shi-yo/
115+
//执行用时:28 ms, 在所有 Swift 提交中击败了74.77%的用户
116+
//内存消耗:13.4 MB, 在所有 Swift 提交中击败了98.28%的用户
117+
func permute(_ nums: [Int]) -> [[Int]] {
118+
var res = [[Int]]()
119+
let count = nums.count
120+
var used = [Int :Bool]()
121+
122+
func backtrack(track: inout [Int]){
123+
if track.count == count{
124+
res.append(track)
125+
}
126+
for num in nums {
127+
if used[num] ?? false {
128+
continue
129+
}
130+
used[num] = true
131+
track.append(num)
132+
backtrack(track: &track)
133+
used[num] = false
134+
track.popLast()
135+
}
136+
}
137+
138+
var items = [Int]()
139+
backtrack( track: &items)
140+
141+
return res
142+
}
143+
```
144+
145+
### [permutations-ii](https://leetcode-cn.com/problems/permutations-ii/)
146+
147+
> 给定一个可包含重复数字的序列,返回所有不重复的全排列
148+
149+
```swift
150+
//https://leetcode-cn.com/problems/permutations-ii/solution/swift-47-quan-pai-lie-iipai-xu-hou-hui-su-you-hua-/
151+
//执行用时:64 ms, 在所有 Swift 提交中击败了43.59%的用户
152+
//内存消耗:14.5 MB, 在所有 Swift 提交中击败了89.47%的用户
153+
func permuteUnique(_ nums: [Int]) -> [[Int]] {
154+
var res = [[Int]]()
155+
let count = nums.count
156+
var used = [Int :Bool]()
157+
let sortedNums = nums.sorted()
158+
159+
160+
func backtrack(track: inout [Int]){
161+
if track.count == count{
162+
res.append(track)
163+
}
164+
for (index,num) in sortedNums.enumerated() {
165+
if used[index] ?? false {
166+
continue
167+
}
168+
169+
// if index > 0 && sortedNums[index] == sortedNums[index - 1] && (used[index - 1] ?? false) {
170+
// continue
171+
// }
172+
//优化:提前剪枝
173+
if index > 0 && sortedNums[index] == sortedNums[index - 1] && !(used[index - 1]!) {
174+
continue
175+
}
176+
177+
used[index] = true
178+
track.append(num)
179+
backtrack(track: &track)
180+
used[index] = false
181+
track.popLast()
182+
}
183+
}
184+
185+
var items = [Int]()
186+
backtrack( track: &items)
187+
188+
return res
189+
}
190+
```
191+
192+
## 练习
193+
194+
- [ ] [subsets](https://leetcode-cn.com/problems/subsets/)
195+
- [ ] [subsets-ii](https://leetcode-cn.com/problems/subsets-ii/)
196+
- [ ] [permutations](https://leetcode-cn.com/problems/permutations/)
197+
- [ ] [permutations-ii](https://leetcode-cn.com/problems/permutations-ii/)
198+
199+
挑战题目
200+
201+
- [ ] [combination-sum](https://leetcode-cn.com/problems/combination-sum/)
202+
- [ ] [letter-combinations-of-a-phone-number](https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/)
203+
- [ ] [palindrome-partitioning](https://leetcode-cn.com/problems/palindrome-partitioning/)
204+
- [ ] [restore-ip-addresses](https://leetcode-cn.com/problems/restore-ip-addresses/)
205+
- [ ] [permutations](https://leetcode-cn.com/problems/permutations/)

0 commit comments

Comments
 (0)