Skip to content

从原项目更新代码 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
eaaf92d
update readme
greyireland Jun 27, 2020
d246680
Update stack_queue.md
thebestwj Jun 28, 2020
bec5a88
Merge pull request #6 from thebestwj/patch-1
greyireland Jun 29, 2020
d8caecf
修复 linked_list.md 里的链接
thebestwj Jun 29, 2020
2324fc6
Merge pull request #8 from thebestwj/patch-2
greyireland Jun 29, 2020
66f34ef
chore(path): use relative
whalecold Jun 29, 2020
083e708
Merge pull request #11 from whalecold/local2
greyireland Jun 30, 2020
31b1566
fix href
greyireland Jun 30, 2020
cee40b3
Merge branch 'master' of github.com:greyireland/algorithm-pattern
greyireland Jun 30, 2020
fa0a942
finished a python version
dashidhy Jul 1, 2020
82fc94d
Merge pull request #14 from dashidhy/patch-2
greyireland Jul 1, 2020
9bf9516
Fix(dp): fix wordBreak solution in dp
FogDong Jul 2, 2020
8723380
Merge pull request #15 from FogDong/master
greyireland Jul 2, 2020
4056779
fix global map
greyireland Jul 2, 2020
b70881e
fix jump-game-ii
greyireland Jul 2, 2020
6259857
add c++ code
binzi56 Jul 30, 2020
2741c8c
Merge pull request #27 from binzi56/hotfix_new
greyireland Aug 3, 2020
4b2f02f
👌 IMPROVE: optimize the reverse function
promacanthus Aug 11, 2020
c297b60
🐛 FIX: expression display error
promacanthus Aug 11, 2020
5c92114
🐛 FIX: comment error
promacanthus Aug 11, 2020
ab5c884
👌 IMPROVE: use bit operations faster
promacanthus Aug 11, 2020
aecf156
👌 IMPROVE:
promacanthus Aug 11, 2020
80a0851
Revert "👌 IMPROVE:"
promacanthus Aug 12, 2020
158486d
Revert "👌 IMPROVE: use bit operations faster"
promacanthus Aug 12, 2020
578c077
Merge pull request #30 from Promacanthus/improve
greyireland Aug 14, 2020
d017eac
添加swift版本
easyui Sep 16, 2020
5e524d2
Merge pull request #32 from easyui/master
greyireland Sep 20, 2020
7c59d9d
Update README.md
lvseouren Mar 10, 2021
0c6abaf
Merge pull request #36 from lvseouren/master
greyireland Mar 10, 2021
f2909c6
Update sort.md
chienmy Mar 26, 2021
caa8928
Update README.md
chienmy Mar 26, 2021
8c74545
Merge pull request #39 from chienmy/master
greyireland Mar 26, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@

### 入门篇 🐶

- [go 语言入门](https://github.com/greyireland/algorithm-pattern/blob/master/introduction/golang.md)
- [算法快速入门](https://github.com/greyireland/algorithm-pattern/blob/master/introduction/quickstart.md)
- [go 语言入门](./introduction/golang.md)
- [算法快速入门](./introduction/quickstart.md)

### 数据结构篇 🐰

- [二叉树](https://github.com/greyireland/algorithm-pattern/blob/master/data_structure/binary_tree.md)
- [链表](https://github.com/greyireland/algorithm-pattern/blob/master/data_structure/linked_list.md)
- [栈和队列](https://github.com/greyireland/algorithm-pattern/blob/master/data_structure/stack_queue.md)
- [二进制](https://github.com/greyireland/algorithm-pattern/blob/master/data_structure/binary_op.md)
- [二叉树](./data_structure/binary_tree.md)
- [链表](./data_structure/linked_list.md)
- [栈和队列](./data_structure/stack_queue.md)
- [二进制](./data_structure/binary_op.md)

### 基础算法篇 🐮

- [二分搜索](https://github.com/greyireland/algorithm-pattern/blob/master/basic_algorithm/binary_search.md)
- [排序算法](https://github.com/greyireland/algorithm-pattern/blob/master/basic_algorithm/sort.md)
- [动态规划](https://github.com/greyireland/algorithm-pattern/blob/master/basic_algorithm/dp.md)
- [二分搜索](./basic_algorithm/binary_search.md)
- [排序算法](./basic_algorithm/sort.md)
- [动态规划](./basic_algorithm/dp.md)

### 算法思维 🦁

- [递归思维](https://github.com/greyireland/algorithm-pattern/blob/master/advanced_algorithm/recursion.md)
- [滑动窗口思想](https://github.com/greyireland/algorithm-pattern/blob/master/advanced_algorithm/slide_window.md)
- [二叉搜索树](https://github.com/greyireland/algorithm-pattern/blob/master/advanced_algorithm/binary_search_tree.md)
- [回溯法](https://github.com/greyireland/algorithm-pattern/blob/master/advanced_algorithm/backtrack.md)
- [递归思维](./advanced_algorithm/recursion.md)
- [滑动窗口思想](./advanced_algorithm/slide_window.md)
- [二叉搜索树](./advanced_algorithm/binary_search_tree.md)
- [回溯法](./advanced_algorithm/backtrack.md)

## 心得体会

Expand Down Expand Up @@ -85,8 +85,21 @@

[我看过的 100 本书](https://github.com/greyireland/awesome-programming-books-1)

## 后续
## 更新计划

持续更新中,觉得还可以的话点个 **star** 收藏呀 ⭐️~

【 Github 】[https://github.com/greyireland/algorithm-pattern](https://github.com/greyireland/algorithm-pattern) ⭐️

## 完成打卡

完成计划之后,可以提交 Pull requests,在下面添加自己的项目仓库,完成自己的算法模板打卡呀~

| 完成 | 用户 | 项目地址 |
| ---- | ------------------------------------------------- | ------------------------------------------------------------------- |
| ✅ | [easyui](https://github.com/easyui/) | [algorithm-pattern-swift(Swift 实现)](https://github.com/easyui/algorithm-pattern-swift),[在线文档 Gitbook](https://zyj.gitbook.io/algorithm-pattern-swift/) |
| ✅ | [wardseptember](https://github.com/wardseptember) | [notes(Java 实现)](https://github.com/wardseptember/notes) |
| ✅ | [dashidhy](https://github.com/dashidhy) | [algorithm-pattern-python(Python 实现)](https://github.com/dashidhy/algorithm-pattern-python) |
| ✅ | [binzi56](https://github.com/binzi56) | [algorithm-pattern-c(c++ 实现)](https://github.com/binzi56/algorithm-pattern-c) |
| ✅ | [lvseouren](https://github.com/lvseouren) | [algorithm-study-record(c++ 实现)](https://github.com/lvseouren/algorithm-study-record) |
| ✅ | [chienmy](https://github.com/chienmy) | [algorithm-pattern-java(Java 实现)](https://github.com/chienmy/algorithm-pattern-java), [在线文档 Gitbook](https://chienmy.gitbook.io/algorithm-pattern-java/) |
42 changes: 34 additions & 8 deletions basic_algorithm/dp.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ func canJump(nums []int) bool {
> 你的目标是使用最少的跳跃次数到达数组的最后一个位置。

```go
// v1动态规划(其他语言超时参考v2)
func jump(nums []int) int {
// 状态:f[i] 表示从起点到当前位置最小次数
// 推导:f[i] = f[j],a[j]+j >=i,min(f[j]+1)
Expand Down Expand Up @@ -383,6 +384,26 @@ func min(a, b int) int {
}
```

```go
// v2 动态规划+贪心优化
func jump(nums []int) int {
n:=len(nums)
f := make([]int, n)
f[0] = 0
for i := 1; i < n; i++ {
// 取第一个能跳到当前位置的点即可
// 因为跳跃次数的结果集是单调递增的,所以贪心思路是正确的
idx:=0
for idx<n&&idx+nums[idx]<i{
idx++
}
f[i]=f[idx]+1
}
return f[n-1]
}

```

### [palindrome-partitioning-ii](https://leetcode-cn.com/problems/palindrome-partitioning-ii/)

> 给定一个字符串 _s_,将 _s_ 分割成一些子串,使每个子串都是回文串。
Expand Down Expand Up @@ -486,32 +507,37 @@ func wordBreak(s string, wordDict []string) bool {
}
f := make([]bool, len(s)+1)
f[0] = true
max := maxLen(wordDict)
max,dict := maxLen(wordDict)
for i := 1; i <= len(s); i++ {
for j := i - max; j < i && j >= 0; j++ {
if f[j] && inDict(s[j:i]) {
l := 0
if i - max > 0 {
l = i - max
}
for j := l; j < i; j++ {
if f[j] && inDict(s[j:i],dict) {
f[i] = true
break
break
}
}
}
return f[len(s)]
}

var dict = make(map[string]bool)

func maxLen(wordDict []string) int {

func maxLen(wordDict []string) (int,map[string]bool) {
dict := make(map[string]bool)
max := 0
for _, v := range wordDict {
dict[v] = true
if len(v) > max {
max = len(v)
}
}
return max
return max,dict
}

func inDict(s string) bool {
func inDict(s string,dict map[string]bool) bool {
_, ok := dict[s]
return ok
}
Expand Down
4 changes: 2 additions & 2 deletions basic_algorithm/sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ func sink(a []int, i int, length int) {
for {
// 左节点索引(从0开始,所以左节点为i*2+1)
l := i*2 + 1
// 有节点索引
// 右节点索引
r := i*2 + 2
// idx保存根、左、右三者之间较大值的索引
idx := i
// 存在左节点,左节点值较大,则取左节点
if l < length && a[l] > a[idx] {
idx = l
}
// 存在有节点,且值较大,取右节点
// 存在右节点,且值较大,取右节点
if r < length && a[r] > a[idx] {
idx = r
}
Expand Down
4 changes: 1 addition & 3 deletions data_structure/binary_tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,7 @@ func zigzagLevelOrder(root *TreeNode) [][]int {
}
func reverse(nums []int) {
for i := 0; i < len(nums)/2; i++ {
t := nums[i]
nums[i] = nums[len(nums)-1-i]
nums[len(nums)-1-i] = t
nums[i], nums[len(nums)-1-i] = nums[len(nums)-1-i], nums[i]
}
}
```
Expand Down
7 changes: 4 additions & 3 deletions data_structure/linked_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ func hasCycle(head *ListNode) bool {
fast := head.Next
slow := head
for fast != nil && fast.Next != nil {
if fast.Val == slow.Val {
// 比较指针是否相等(不要使用val比较!)
if fast == slow {
return true
}
fast = fast.Next.Next
Expand All @@ -389,7 +390,7 @@ func hasCycle(head *ListNode) bool {
}
```

### [linked-list-cycle-ii](https://leetcode-cn.com/problems/https://leetcode-cn.com/problems/linked-list-cycle-ii/)
### [linked-list-cycle-ii](https://leetcode-cn.com/problems/linked-list-cycle-ii/)

> 给定一个链表,返回链表开始入环的第一个节点。  如果链表无环,则返回  `null`。

Expand Down Expand Up @@ -581,6 +582,6 @@ func copyRandomList(head *Node) *Node {
- [ ] [sort-list](https://leetcode-cn.com/problems/sort-list/)
- [ ] [reorder-list](https://leetcode-cn.com/problems/reorder-list/)
- [ ] [linked-list-cycle](https://leetcode-cn.com/problems/linked-list-cycle/)
- [ ] [linked-list-cycle-ii](https://leetcode-cn.com/problems/https://leetcode-cn.com/problems/linked-list-cycle-ii/)
- [ ] [linked-list-cycle-ii](https://leetcode-cn.com/problems/linked-list-cycle-ii/)
- [ ] [palindrome-linked-list](https://leetcode-cn.com/problems/palindrome-linked-list/)
- [ ] [copy-list-with-random-pointer](https://leetcode-cn.com/problems/copy-list-with-random-pointer/)
9 changes: 5 additions & 4 deletions data_structure/stack_queue.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ func (this *MinStack) GetMin() int {

[evaluate-reverse-polish-notation](https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/)

> **波兰表达式计算** > **输入:** ["2", "1", "+", "3", "*"] > **输出:** 9
> **解释:** ((2 + 1) \* 3) = 9
> **波兰表达式计算** > **输入:** `["2", "1", "+", "3", "*"]` > **输出:** 9
>
> **解释:** `((2 + 1) * 3) = 9`

思路:通过栈保存原来的元素,遇到表达式弹出运算,再推入结果,重复这个过程

Expand All @@ -100,7 +101,7 @@ func evalRPN(tokens []string) int {
if len(stack)<2{
return -1
}
// 注意:a为除数,b为被除数
// 注意:a为被除数,b为除数
b:=stack[len(stack)-1]
a:=stack[len(stack)-2]
stack=stack[:len(stack)-2]
Expand Down Expand Up @@ -513,7 +514,7 @@ func updateMatrix(matrix [][]int) [][]int {
## 总结

- 熟悉栈的使用场景
- 后出先出,保存临时值
- 后入先出,保存临时值
- 利用栈 DFS 深度搜索
- 熟悉队列的使用场景
- 利用队列 BFS 广度搜索
Expand Down