From eaaf92de2742b4e2a06fa3e307dd83579a31dbb8 Mon Sep 17 00:00:00 2001 From: greyireland Date: Sat, 27 Jun 2020 19:44:54 +0800 Subject: [PATCH 01/26] update readme --- README.md | 11 ++++++++++- data_structure/linked_list.md | 3 ++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7f3e22b4..7395a655 100644 --- a/README.md +++ b/README.md @@ -85,8 +85,17 @@ [我看过的 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,在下面添加自己的项目仓库,完成自己的算法模板打卡呀~ + +| 完成 | 用户 | 项目地址 | +| ---- | ------------------------------------------------- | ------------------------------------------------------------------- | +| ✅ | [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) | diff --git a/data_structure/linked_list.md b/data_structure/linked_list.md index ebcc1625..8f4a969b 100644 --- a/data_structure/linked_list.md +++ b/data_structure/linked_list.md @@ -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 From d246680a85a3bd0d29d1138b0e82613ef024928b Mon Sep 17 00:00:00 2001 From: thebestwj <55725208+thebestwj@users.noreply.github.com> Date: Sun, 28 Jun 2020 09:46:54 +0800 Subject: [PATCH 02/26] Update stack_queue.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 一个小错误 --- data_structure/stack_queue.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structure/stack_queue.md b/data_structure/stack_queue.md index e1ed723c..6f90a3ef 100644 --- a/data_structure/stack_queue.md +++ b/data_structure/stack_queue.md @@ -513,7 +513,7 @@ func updateMatrix(matrix [][]int) [][]int { ## 总结 - 熟悉栈的使用场景 - - 后出先出,保存临时值 + - 后入先出,保存临时值 - 利用栈 DFS 深度搜索 - 熟悉队列的使用场景 - 利用队列 BFS 广度搜索 From d8caecf530a6b066a7e7fefbdaac4446ca70dabd Mon Sep 17 00:00:00 2001 From: thebestwj <55725208+thebestwj@users.noreply.github.com> Date: Mon, 29 Jun 2020 12:55:16 +0800 Subject: [PATCH 03/26] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20linked=5Flist.md=20?= =?UTF-8?q?=E9=87=8C=E7=9A=84=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data_structure/linked_list.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structure/linked_list.md b/data_structure/linked_list.md index 8f4a969b..db13680d 100644 --- a/data_structure/linked_list.md +++ b/data_structure/linked_list.md @@ -582,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/) From 66f34ef026a7e18751e7d9007a6b3c7d7ee260d1 Mon Sep 17 00:00:00 2001 From: Lisheng Zheng Date: Tue, 30 Jun 2020 01:41:25 +0800 Subject: [PATCH 04/26] chore(path): use relative --- README.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 7395a655..e76117be 100644 --- a/README.md +++ b/README.md @@ -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) ## 心得体会 From 31b156684b2e927434f81dd9f50679c1bfd8da7b Mon Sep 17 00:00:00 2001 From: greyireland Date: Tue, 30 Jun 2020 21:27:16 +0800 Subject: [PATCH 05/26] fix href --- data_structure/linked_list.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structure/linked_list.md b/data_structure/linked_list.md index 8f4a969b..93127a11 100644 --- a/data_structure/linked_list.md +++ b/data_structure/linked_list.md @@ -390,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`。 From fa0a942c366d2d44529d5bd491fa85bd1ec38200 Mon Sep 17 00:00:00 2001 From: Hongyuan Du Date: Wed, 1 Jul 2020 01:56:32 -0700 Subject: [PATCH 06/26] finished a python version --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e76117be..a09ea4f9 100644 --- a/README.md +++ b/README.md @@ -98,4 +98,4 @@ | 完成 | 用户 | 项目地址 | | ---- | ------------------------------------------------- | ------------------------------------------------------------------- | | ✅ | [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) | +| ✅ | [dashidhy](https://github.com/dashidhy) | [algorithm-pattern-python(Python 实现)](https://github.com/dashidhy/algorithm-pattern-python) | From 9bf951640d8ac2fee56de51d654a9c63fe7dc564 Mon Sep 17 00:00:00 2001 From: Tianxin Dong Date: Thu, 2 Jul 2020 17:24:53 +0800 Subject: [PATCH 07/26] Fix(dp): fix wordBreak solution in dp --- basic_algorithm/dp.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/basic_algorithm/dp.md b/basic_algorithm/dp.md index d251ca7d..9ada08b4 100644 --- a/basic_algorithm/dp.md +++ b/basic_algorithm/dp.md @@ -488,7 +488,11 @@ func wordBreak(s string, wordDict []string) bool { f[0] = true max := maxLen(wordDict) for i := 1; i <= len(s); i++ { - for j := i - max; j < i && j >= 0; j++ { + l := 0 + if i - max > 0 { + l = i - max + } + for j := l; j < i; j++ { if f[j] && inDict(s[j:i]) { f[i] = true break From 40567799e0e815378dc18f16713affac71261a72 Mon Sep 17 00:00:00 2001 From: greyireland Date: Thu, 2 Jul 2020 19:36:10 +0800 Subject: [PATCH 08/26] fix global map --- basic_algorithm/dp.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/basic_algorithm/dp.md b/basic_algorithm/dp.md index 9ada08b4..07f0b018 100644 --- a/basic_algorithm/dp.md +++ b/basic_algorithm/dp.md @@ -486,25 +486,26 @@ 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++ { l := 0 if i - max > 0 { l = i - max } for j := l; j < i; j++ { - if f[j] && inDict(s[j:i]) { + 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 @@ -512,10 +513,10 @@ func maxLen(wordDict []string) int { 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 } From b70881e3333dcfbd2e42791fba75b0223f81ffa4 Mon Sep 17 00:00:00 2001 From: greyireland Date: Thu, 2 Jul 2020 23:15:43 +0800 Subject: [PATCH 09/26] fix jump-game-ii --- basic_algorithm/dp.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/basic_algorithm/dp.md b/basic_algorithm/dp.md index 07f0b018..3bdee61b 100644 --- a/basic_algorithm/dp.md +++ b/basic_algorithm/dp.md @@ -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) @@ -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 给定一个字符串 _s_,将 _s_ 分割成一些子串,使每个子串都是回文串。 From 6259857052bb1ae96b6f2c9960858970c142b6a1 Mon Sep 17 00:00:00 2001 From: shuaibin <1533607721@qq.com> Date: Thu, 30 Jul 2020 11:53:35 +0800 Subject: [PATCH 10/26] add c++ code --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a09ea4f9..3d158d31 100644 --- a/README.md +++ b/README.md @@ -99,3 +99,4 @@ | ---- | ------------------------------------------------- | ------------------------------------------------------------------- | | ✅ | [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) | From 4b2f02fa7cee9cac49f3aca5563abea682830b7e Mon Sep 17 00:00:00 2001 From: sugoi Date: Tue, 11 Aug 2020 14:27:49 +0800 Subject: [PATCH 11/26] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20optimize=20the?= =?UTF-8?q?=20reverse=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data_structure/binary_tree.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/data_structure/binary_tree.md b/data_structure/binary_tree.md index d01a8ca4..b5c84b49 100644 --- a/data_structure/binary_tree.md +++ b/data_structure/binary_tree.md @@ -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] } } ``` From c297b60450260604a010ed79c37144bdbc57a044 Mon Sep 17 00:00:00 2001 From: sugoi Date: Tue, 11 Aug 2020 16:22:20 +0800 Subject: [PATCH 12/26] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20expression=20displa?= =?UTF-8?q?y=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data_structure/stack_queue.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/data_structure/stack_queue.md b/data_structure/stack_queue.md index 6f90a3ef..59d41d80 100644 --- a/data_structure/stack_queue.md +++ b/data_structure/stack_queue.md @@ -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` 思路:通过栈保存原来的元素,遇到表达式弹出运算,再推入结果,重复这个过程 From 5c921147c555843739929e0f41e497e4022e755e Mon Sep 17 00:00:00 2001 From: sugoi Date: Tue, 11 Aug 2020 16:31:42 +0800 Subject: [PATCH 13/26] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20comment=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data_structure/stack_queue.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structure/stack_queue.md b/data_structure/stack_queue.md index 59d41d80..bc4e335d 100644 --- a/data_structure/stack_queue.md +++ b/data_structure/stack_queue.md @@ -101,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] From ab5c88403c93670ea6429559ef67fe452547324a Mon Sep 17 00:00:00 2001 From: sugoi Date: Tue, 11 Aug 2020 18:49:13 +0800 Subject: [PATCH 14/26] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20use=20bit=20ope?= =?UTF-8?q?rations=20faster?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- basic_algorithm/binary_search.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/basic_algorithm/binary_search.md b/basic_algorithm/binary_search.md index 15f336a3..b352d5de 100644 --- a/basic_algorithm/binary_search.md +++ b/basic_algorithm/binary_search.md @@ -27,7 +27,7 @@ func search(nums []int, target int) int { end := len(nums) - 1 // 2、处理for循环 for start+1 < end { - mid := start + (end-start)/2 + mid := start + ((end-start)>>1) // 3、比较a[mid]和target值 if nums[mid] == target { end = mid @@ -64,7 +64,7 @@ func search(nums []int, target int) int { start := 0 end := len(nums) - 1 for start <= end { - mid := start + (end-start)/2 + mid := start + ((end-start)>>1) if nums[mid] == target { return mid } else if nums[mid] < target { @@ -98,7 +98,7 @@ func searchRange (A []int, target int) []int { start := 0 end := len(A) - 1 for start+1 < end { - mid := start + (end-start)/2 + mid := start + ((end-start)>>1) if A[mid] > target { end = mid } else if A[mid] < target { @@ -121,7 +121,7 @@ func searchRange (A []int, target int) []int { start = 0 end = len(A) - 1 for start+1 < end { - mid := start + (end-start)/2 + mid := start + ((end-start)>>1) if A[mid] > target { end = mid } else if A[mid] < target { @@ -155,7 +155,7 @@ func searchInsert(nums []int, target int) int { start := 0 end := len(nums) - 1 for start+1 < end { - mid := start + (end-start)/2 + mid := start + ((end-start)>>1) if nums[mid] == target { // 标记开始位置 start = mid @@ -194,7 +194,7 @@ func searchMatrix(matrix [][]int, target int) bool { start := 0 end := row*col - 1 for start+1 < end { - mid := start + (end-start)/2 + mid := start + ((end-start)>>1) // 获取2纬数组对应值 val := matrix[mid/col][mid%col] if val > target { @@ -252,7 +252,7 @@ func findMin(nums []int) int { end := len(nums) - 1 for start+1 < end { - mid := start + (end-start)/2 + mid := start + ((end-start)>>1) // 最后一个元素值为target if nums[mid] <= nums[end] { end = mid @@ -289,7 +289,7 @@ func findMin(nums []int) int { for start < end && nums[start] == nums[start+1] { start++ } - mid := start + (end-start)/2 + mid := start + ((end-start)>>1) // 中间元素和最后一个元素比较(判断中间点落在左边上升区,还是右边上升区) if nums[mid] <= nums[end] { end = mid @@ -320,7 +320,7 @@ func search(nums []int, target int) int { start := 0 end := len(nums) - 1 for start+1 < end { - mid := start + (end-start)/2 + mid := start + ((end-start)>>1) // 相等直接返回 if nums[mid] == target { return mid @@ -375,7 +375,7 @@ func search(nums []int, target int) bool { for start < end && nums[end] == nums[end-1] { end-- } - mid := start + (end-start)/2 + mid := start + ((end-start)>>1) // 相等直接返回 if nums[mid] == target { return true From aecf1564332dda30b80d1558d4e77149832f6e00 Mon Sep 17 00:00:00 2001 From: sugoi Date: Tue, 11 Aug 2020 18:53:38 +0800 Subject: [PATCH 15/26] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- basic_algorithm/binary_search.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basic_algorithm/binary_search.md b/basic_algorithm/binary_search.md index b352d5de..f0f7d9d5 100644 --- a/basic_algorithm/binary_search.md +++ b/basic_algorithm/binary_search.md @@ -223,7 +223,7 @@ func firstBadVersion(n int) int { start := 0 end := n for start+1 < end { - mid := start + (end - start)/2 + mid := start + ((end-start)>>1) if isBadVersion(mid) { end = mid } else if isBadVersion(mid) == false { From 80a0851574baaebcab68028f49f46b6e96c756e6 Mon Sep 17 00:00:00 2001 From: sugoi Date: Wed, 12 Aug 2020 15:28:32 +0800 Subject: [PATCH 16/26] =?UTF-8?q?Revert=20"=F0=9F=91=8C=20IMPROVE:"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit aecf1564332dda30b80d1558d4e77149832f6e00. --- basic_algorithm/binary_search.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basic_algorithm/binary_search.md b/basic_algorithm/binary_search.md index f0f7d9d5..b352d5de 100644 --- a/basic_algorithm/binary_search.md +++ b/basic_algorithm/binary_search.md @@ -223,7 +223,7 @@ func firstBadVersion(n int) int { start := 0 end := n for start+1 < end { - mid := start + ((end-start)>>1) + mid := start + (end - start)/2 if isBadVersion(mid) { end = mid } else if isBadVersion(mid) == false { From 158486d23cdea8d40083aa1a00aacac4121f3f43 Mon Sep 17 00:00:00 2001 From: sugoi Date: Wed, 12 Aug 2020 15:29:37 +0800 Subject: [PATCH 17/26] =?UTF-8?q?Revert=20"=F0=9F=91=8C=20IMPROVE:=20use?= =?UTF-8?q?=20bit=20operations=20faster"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit ab5c88403c93670ea6429559ef67fe452547324a. --- basic_algorithm/binary_search.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/basic_algorithm/binary_search.md b/basic_algorithm/binary_search.md index b352d5de..15f336a3 100644 --- a/basic_algorithm/binary_search.md +++ b/basic_algorithm/binary_search.md @@ -27,7 +27,7 @@ func search(nums []int, target int) int { end := len(nums) - 1 // 2、处理for循环 for start+1 < end { - mid := start + ((end-start)>>1) + mid := start + (end-start)/2 // 3、比较a[mid]和target值 if nums[mid] == target { end = mid @@ -64,7 +64,7 @@ func search(nums []int, target int) int { start := 0 end := len(nums) - 1 for start <= end { - mid := start + ((end-start)>>1) + mid := start + (end-start)/2 if nums[mid] == target { return mid } else if nums[mid] < target { @@ -98,7 +98,7 @@ func searchRange (A []int, target int) []int { start := 0 end := len(A) - 1 for start+1 < end { - mid := start + ((end-start)>>1) + mid := start + (end-start)/2 if A[mid] > target { end = mid } else if A[mid] < target { @@ -121,7 +121,7 @@ func searchRange (A []int, target int) []int { start = 0 end = len(A) - 1 for start+1 < end { - mid := start + ((end-start)>>1) + mid := start + (end-start)/2 if A[mid] > target { end = mid } else if A[mid] < target { @@ -155,7 +155,7 @@ func searchInsert(nums []int, target int) int { start := 0 end := len(nums) - 1 for start+1 < end { - mid := start + ((end-start)>>1) + mid := start + (end-start)/2 if nums[mid] == target { // 标记开始位置 start = mid @@ -194,7 +194,7 @@ func searchMatrix(matrix [][]int, target int) bool { start := 0 end := row*col - 1 for start+1 < end { - mid := start + ((end-start)>>1) + mid := start + (end-start)/2 // 获取2纬数组对应值 val := matrix[mid/col][mid%col] if val > target { @@ -252,7 +252,7 @@ func findMin(nums []int) int { end := len(nums) - 1 for start+1 < end { - mid := start + ((end-start)>>1) + mid := start + (end-start)/2 // 最后一个元素值为target if nums[mid] <= nums[end] { end = mid @@ -289,7 +289,7 @@ func findMin(nums []int) int { for start < end && nums[start] == nums[start+1] { start++ } - mid := start + ((end-start)>>1) + mid := start + (end-start)/2 // 中间元素和最后一个元素比较(判断中间点落在左边上升区,还是右边上升区) if nums[mid] <= nums[end] { end = mid @@ -320,7 +320,7 @@ func search(nums []int, target int) int { start := 0 end := len(nums) - 1 for start+1 < end { - mid := start + ((end-start)>>1) + mid := start + (end-start)/2 // 相等直接返回 if nums[mid] == target { return mid @@ -375,7 +375,7 @@ func search(nums []int, target int) bool { for start < end && nums[end] == nums[end-1] { end-- } - mid := start + ((end-start)>>1) + mid := start + (end-start)/2 // 相等直接返回 if nums[mid] == target { return true From d017eac0ec69794cfb7529f8c2f496e3f22b02a8 Mon Sep 17 00:00:00 2001 From: easyui Date: Wed, 16 Sep 2020 10:28:22 +0800 Subject: [PATCH 18/26] =?UTF-8?q?=E6=B7=BB=E5=8A=A0swift=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3d158d31..f26c57c0 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,7 @@ | 完成 | 用户 | 项目地址 | | ---- | ------------------------------------------------- | ------------------------------------------------------------------- | +| ✅ | [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) | From 7c59d9dfa152da0f06b9976cddc4b58a75205c99 Mon Sep 17 00:00:00 2001 From: lvseouren Date: Wed, 10 Mar 2021 18:25:19 +0800 Subject: [PATCH 19/26] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f26c57c0..5022cb95 100644 --- a/README.md +++ b/README.md @@ -101,3 +101,4 @@ | ✅ | [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) | From f2909c6e0e61c3d71cd6962ca05b1ae0cf1c265f Mon Sep 17 00:00:00 2001 From: chienmy Date: Fri, 26 Mar 2021 17:35:09 +0800 Subject: [PATCH 20/26] Update sort.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改错字 --- basic_algorithm/sort.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/basic_algorithm/sort.md b/basic_algorithm/sort.md index 9eaa2cb8..24ae939d 100644 --- a/basic_algorithm/sort.md +++ b/basic_algorithm/sort.md @@ -121,7 +121,7 @@ func sink(a []int, i int, length int) { for { // 左节点索引(从0开始,所以左节点为i*2+1) l := i*2 + 1 - // 有节点索引 + // 右节点索引 r := i*2 + 2 // idx保存根、左、右三者之间较大值的索引 idx := i @@ -129,7 +129,7 @@ func sink(a []int, i int, length int) { if l < length && a[l] > a[idx] { idx = l } - // 存在有节点,且值较大,取右节点 + // 存在右节点,且值较大,取右节点 if r < length && a[r] > a[idx] { idx = r } From caa89281e936766f1b81599a9c68eb2a117dbfe9 Mon Sep 17 00:00:00 2001 From: chienmy Date: Fri, 26 Mar 2021 17:37:23 +0800 Subject: [PATCH 21/26] Update README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 打卡 --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5022cb95..ee285640 100644 --- a/README.md +++ b/README.md @@ -102,3 +102,4 @@ | ✅ | [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/) | From a445d0732c40606c34b48b1664fdd9f330d48541 Mon Sep 17 00:00:00 2001 From: ligecarryme <965108861@qq.com> Date: Fri, 14 May 2021 00:17:40 +0800 Subject: [PATCH 22/26] update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ee285640..c726cd5e 100644 --- a/README.md +++ b/README.md @@ -103,3 +103,5 @@ | ✅ | [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/) | +| ✅ | [ligecarryme](https://github.com/ligecarryme) | [algorithm-pattern-JavaScript(JS+TS实现)](https://github.com/ligecarryme/algorithm-pattern-JavaScript) | + From 1a004aa84379932d4b820c5aea8f6f3302a2af5a Mon Sep 17 00:00:00 2001 From: greyireland Date: Sat, 21 Jan 2023 11:43:02 +0800 Subject: [PATCH 23/26] Create LICENSE --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..60cb8f09 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 greyireland + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From 7d093604d10eec0452e3ba80d9856066838009e2 Mon Sep 17 00:00:00 2001 From: Rui Min Date: Thu, 9 Mar 2023 16:33:37 +0800 Subject: [PATCH 24/26] add dart code --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c726cd5e..66d4a588 100644 --- a/README.md +++ b/README.md @@ -104,4 +104,4 @@ | ✅ | [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/) | | ✅ | [ligecarryme](https://github.com/ligecarryme) | [algorithm-pattern-JavaScript(JS+TS实现)](https://github.com/ligecarryme/algorithm-pattern-JavaScript) | - +| ✅ | [Esdeath](https://github.com/Esdeath) | [algorithm-pattern-dart(dart实现)](https://github.com/Esdeath/algorithm-pattern-dart),[在线文档 Gitbook](https://ayaseeri.gitbook.io/algorithm-pattern-dart/) | From f367ce3da8fb600cb807c27ca54b578d4a856a55 Mon Sep 17 00:00:00 2001 From: Robin Date: Sat, 9 Dec 2023 23:40:07 +0800 Subject: [PATCH 25/26] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 66d4a588..c04b5b2a 100644 --- a/README.md +++ b/README.md @@ -105,3 +105,4 @@ | ✅ | [chienmy](https://github.com/chienmy) | [algorithm-pattern-java(Java 实现)](https://github.com/chienmy/algorithm-pattern-java), [在线文档 Gitbook](https://chienmy.gitbook.io/algorithm-pattern-java/) | | ✅ | [ligecarryme](https://github.com/ligecarryme) | [algorithm-pattern-JavaScript(JS+TS实现)](https://github.com/ligecarryme/algorithm-pattern-JavaScript) | | ✅ | [Esdeath](https://github.com/Esdeath) | [algorithm-pattern-dart(dart实现)](https://github.com/Esdeath/algorithm-pattern-dart),[在线文档 Gitbook](https://ayaseeri.gitbook.io/algorithm-pattern-dart/) | +| ✅ | [longpi1](https://github.com/longpi1) | [algorithm-pattern-golang(golang实现)](https://github.com/longpi1/algorithm-pattern) From 9660de995e5c580ff8a510914b8fcf78bc893432 Mon Sep 17 00:00:00 2001 From: tpxxn <351765204@qq.com> Date: Fri, 22 Dec 2023 18:04:31 +0800 Subject: [PATCH 26/26] add csharp code --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c04b5b2a..39c073be 100644 --- a/README.md +++ b/README.md @@ -106,3 +106,4 @@ | ✅ | [ligecarryme](https://github.com/ligecarryme) | [algorithm-pattern-JavaScript(JS+TS实现)](https://github.com/ligecarryme/algorithm-pattern-JavaScript) | | ✅ | [Esdeath](https://github.com/Esdeath) | [algorithm-pattern-dart(dart实现)](https://github.com/Esdeath/algorithm-pattern-dart),[在线文档 Gitbook](https://ayaseeri.gitbook.io/algorithm-pattern-dart/) | | ✅ | [longpi1](https://github.com/longpi1) | [algorithm-pattern-golang(golang实现)](https://github.com/longpi1/algorithm-pattern) +| ✅ | [tpxxn](https://github.com/tpxxn) | [algorithm-pattern-CSharp(C# 实现)](https://github.com/tpxxn/algorithm-pattern-CSharp) \ No newline at end of file