diff --git a/Algorithms/0016. 3Sum Closest/16. 3Sum Closest.go b/Algorithms/0016. 3Sum Closest/16. 3Sum Closest.go index aa0c03bb..0c3db8eb 100644 --- a/Algorithms/0016. 3Sum Closest/16. 3Sum Closest.go +++ b/Algorithms/0016. 3Sum Closest/16. 3Sum Closest.go @@ -5,6 +5,7 @@ import ( "sort" ) +// 解法一 O(n^2) func threeSumClosest(nums []int, target int) int { n, res, diff := len(nums), 0, math.MaxInt32 if n > 2 { @@ -28,6 +29,7 @@ func threeSumClosest(nums []int, target int) int { return res } +// 解法二 暴力解法 O(n^3) func threeSumClosest_(nums []int, target int) int { res, difference := 0, math.MaxInt16 for i := 0; i < len(nums); i++ { diff --git a/Algorithms/0019. Remove Nth Node From End of List/19. Remove Nth Node From End of List.go b/Algorithms/0019. Remove Nth Node From End of List/19. Remove Nth Node From End of List.go index 80ae7e27..5642bcc7 100644 --- a/Algorithms/0019. Remove Nth Node From End of List/19. Remove Nth Node From End of List.go +++ b/Algorithms/0019. Remove Nth Node From End of List/19. Remove Nth Node From End of List.go @@ -8,6 +8,7 @@ package leetcode * } */ +// 解法一 func removeNthFromEnd(head *ListNode, n int) *ListNode { var fast, slow *ListNode fast = head @@ -27,6 +28,7 @@ func removeNthFromEnd(head *ListNode, n int) *ListNode { return head } +// 解法二 func removeNthFromEnd_(head *ListNode, n int) *ListNode { if head == nil { return nil diff --git a/Algorithms/0026. Remove Duplicates from Sorted Array/26. Remove Duplicates from Sorted Array.go b/Algorithms/0026. Remove Duplicates from Sorted Array/26. Remove Duplicates from Sorted Array.go index 7769c06e..8cdd3c02 100644 --- a/Algorithms/0026. Remove Duplicates from Sorted Array/26. Remove Duplicates from Sorted Array.go +++ b/Algorithms/0026. Remove Duplicates from Sorted Array/26. Remove Duplicates from Sorted Array.go @@ -1,5 +1,6 @@ package leetcode +// 解法一 func removeDuplicates(nums []int) int { if len(nums) == 0 { return 0 @@ -18,6 +19,7 @@ func removeDuplicates(nums []int) int { return last + 1 } +// 解法二 func removeDuplicates_(nums []int) int { if len(nums) == 0 { return 0 diff --git a/Algorithms/0028. Implement strStr()/28. Implement strStr().go b/Algorithms/0028. Implement strStr()/28. Implement strStr().go index b4db8b21..9b033e39 100644 --- a/Algorithms/0028. Implement strStr()/28. Implement strStr().go +++ b/Algorithms/0028. Implement strStr()/28. Implement strStr().go @@ -2,6 +2,7 @@ package leetcode import "strings" +// 解法一 func strStr(haystack string, needle string) int { for i := 0; ; i++ { for j := 0; ; j++ { @@ -18,6 +19,7 @@ func strStr(haystack string, needle string) int { } } +// 解法二 func strStr_(haystack string, needle string) int { return strings.Index(haystack, needle) } diff --git a/Algorithms/0086. Partition List/86. Partition List.go b/Algorithms/0086. Partition List/86. Partition List.go index 9c72561a..58b745b9 100644 --- a/Algorithms/0086. Partition List/86. Partition List.go +++ b/Algorithms/0086. Partition List/86. Partition List.go @@ -7,6 +7,8 @@ package leetcode * Next *ListNode * } */ + +// 解法一 单链表 func partition(head *ListNode, x int) *ListNode { beforeHead := &ListNode{Val: 0, Next: nil} before := beforeHead @@ -34,6 +36,7 @@ type DoublyListNode struct { Next *DoublyListNode } +// 解法二 双链表 func partition_(head *ListNode, x int) *ListNode { if head == nil || head.Next == nil { return head diff --git a/Algorithms/0143. Reorder List/143. Reorder List.go b/Algorithms/0143. Reorder List/143. Reorder List.go index 9fef7121..68a31c1b 100644 --- a/Algorithms/0143. Reorder List/143. Reorder List.go +++ b/Algorithms/0143. Reorder List/143. Reorder List.go @@ -7,6 +7,8 @@ package leetcode * Next *ListNode * } */ + +// 解法一 单链表 func reorderList(head *ListNode) *ListNode { if head == nil || head.Next == nil { return head @@ -43,6 +45,7 @@ func reorderList(head *ListNode) *ListNode { return head } +// 解法二 数组 func reorderList_1(head *ListNode) *ListNode { array := listToArray(head) length := len(array) diff --git a/Algorithms/0144. Binary Tree Preorder Traversal/144. Binary Tree Preorder Traversal.go b/Algorithms/0144. Binary Tree Preorder Traversal/144. Binary Tree Preorder Traversal.go index a4bb7615..ba0cc501 100644 --- a/Algorithms/0144. Binary Tree Preorder Traversal/144. Binary Tree Preorder Traversal.go +++ b/Algorithms/0144. Binary Tree Preorder Traversal/144. Binary Tree Preorder Traversal.go @@ -8,6 +8,8 @@ package leetcode * Right *TreeNode * } */ + +// 解法一 func preorderTraversal(root *TreeNode) []int { res := []int{} if root != nil { @@ -24,6 +26,7 @@ func preorderTraversal(root *TreeNode) []int { return res } +// 解法二 func preorderTraversal_(root *TreeNode) []int { var result []int preorder(root, &result) diff --git a/Algorithms/0148. Sort List/README.md b/Algorithms/0148. Sort List/README.md index d0b3383c..3153f7ef 100644 --- a/Algorithms/0148. Sort List/README.md +++ b/Algorithms/0148. Sort List/README.md @@ -24,4 +24,4 @@ Output: -1->0->3->4->5 ## 解题思路 -这道题只能用归并排序才能符合要求。归并排序需要的 2 个操作在其他题目已经出现过了,取中间点,第 876 题,合并 2 个有序链表,第 21 题。 \ No newline at end of file +这道题只能用归并排序才能符合要求。归并排序需要的 2 个操作在其他题目已经出现过了,取中间点是第 876 题,合并 2 个有序链表是第 21 题。 \ No newline at end of file diff --git a/Algorithms/0164. Maximum Gap/164. Maximum Gap.go b/Algorithms/0164. Maximum Gap/164. Maximum Gap.go index d1ac0420..ceac05de 100644 --- a/Algorithms/0164. Maximum Gap/164. Maximum Gap.go +++ b/Algorithms/0164. Maximum Gap/164. Maximum Gap.go @@ -1,5 +1,6 @@ package leetcode +// 解法一 func maximumGap(nums []int) int { if len(nums) < 2 { return 0 @@ -36,6 +37,7 @@ func quickSort__(a []int, lo, hi int) { quickSort__(a, p+1, hi) } +// 解法二 func maximumGap_(nums []int) int { if nums == nil || len(nums) < 2 { diff --git a/Algorithms/0167. Two Sum II - Input array is sorted/167. Two Sum II - Input array is sorted.go b/Algorithms/0167. Two Sum II - Input array is sorted/167. Two Sum II - Input array is sorted.go index 5d80c074..17a289f8 100644 --- a/Algorithms/0167. Two Sum II - Input array is sorted/167. Two Sum II - Input array is sorted.go +++ b/Algorithms/0167. Two Sum II - Input array is sorted/167. Two Sum II - Input array is sorted.go @@ -1,6 +1,6 @@ package leetcode -// 这一题可以利用数组有序的特性 +// 解法一 这一题可以利用数组有序的特性 func twoSum_(numbers []int, target int) []int { i, j := 0, len(numbers)-1 for i < j { @@ -15,7 +15,7 @@ func twoSum_(numbers []int, target int) []int { return []int{-1, -1} } -// 不管数组是否有序,空间复杂度比上一种解法要多 O(n) +// 解法二 不管数组是否有序,空间复杂度比上一种解法要多 O(n) func twoSum__(numbers []int, target int) []int { m := make(map[int]int) for i := 0; i < len(numbers); i++ { diff --git a/Algorithms/0215. Kth Largest Element in an Array/215. Kth Largest Element in an Array.go b/Algorithms/0215. Kth Largest Element in an Array/215. Kth Largest Element in an Array.go index ff19b908..236dc5eb 100644 --- a/Algorithms/0215. Kth Largest Element in an Array/215. Kth Largest Element in an Array.go +++ b/Algorithms/0215. Kth Largest Element in an Array/215. Kth Largest Element in an Array.go @@ -2,13 +2,13 @@ package leetcode import "sort" -// 排序的方法反而速度是最快的 +// 解法一 排序,排序的方法反而速度是最快的 func findKthLargest_(nums []int, k int) int { sort.Ints(nums) return nums[len(nums)-k] } -// 这个方法的理论依据是 partition 得到的点的下标就是最终排序之后的下标,根据这个下标,我们可以判断第 K 大的数在哪里 +// 解法二 这个方法的理论依据是 partition 得到的点的下标就是最终排序之后的下标,根据这个下标,我们可以判断第 K 大的数在哪里 func findKthLargest(nums []int, k int) int { if len(nums) == 0 { return 0 diff --git a/Algorithms/0242. Valid Anagram/242. Valid Anagram.go b/Algorithms/0242. Valid Anagram/242. Valid Anagram.go index b16b09e9..0531f39c 100644 --- a/Algorithms/0242. Valid Anagram/242. Valid Anagram.go +++ b/Algorithms/0242. Valid Anagram/242. Valid Anagram.go @@ -1,5 +1,6 @@ package leetcode +// 解法一 func isAnagram(s string, t string) bool { alphabet := make([]int, 26) sBytes := []byte(s) @@ -20,6 +21,8 @@ func isAnagram(s string, t string) bool { } return true } + +// 解法二 func isAnagram_(s string, t string) bool { if s == "" && t == "" { return true diff --git a/Algorithms/0274. H-Index/274. H-Index.go b/Algorithms/0274. H-Index/274. H-Index.go index c29de8dd..35e59317 100644 --- a/Algorithms/0274. H-Index/274. H-Index.go +++ b/Algorithms/0274. H-Index/274. H-Index.go @@ -22,7 +22,7 @@ func hIndex(citations []int) int { } // 解法二 -func hIndex__(citations []int) int { +func hIndex_(citations []int) int { quickSort__(citations, 0, len(citations)-1) hIndex := 0 for i := len(citations) - 1; i >= 0; i-- { diff --git a/Algorithms/0287. Find the Duplicate Number/287. Find the Duplicate Number.go b/Algorithms/0287. Find the Duplicate Number/287. Find the Duplicate Number.go index 00a7b14f..d7cdb98e 100644 --- a/Algorithms/0287. Find the Duplicate Number/287. Find the Duplicate Number.go +++ b/Algorithms/0287. Find the Duplicate Number/287. Find the Duplicate Number.go @@ -2,6 +2,7 @@ package leetcode import "sort" +// 解法一 func findDuplicate(nums []int) int { slow := nums[0] fast := nums[nums[0]] @@ -17,6 +18,7 @@ func findDuplicate(nums []int) int { return walker } +// 解法二 func findDuplicate_(nums []int) int { if len(nums) == 0 { return 0 diff --git a/Algorithms/0324. Wiggle Sort II/324. Wiggle Sort II.go b/Algorithms/0324. Wiggle Sort II/324. Wiggle Sort II.go index 9093e661..c0c3dc85 100644 --- a/Algorithms/0324. Wiggle Sort II/324. Wiggle Sort II.go +++ b/Algorithms/0324. Wiggle Sort II/324. Wiggle Sort II.go @@ -4,6 +4,7 @@ import ( "sort" ) +// 解法一 func wiggleSort(nums []int) { if len(nums) < 2 { return @@ -64,6 +65,7 @@ func partition__324(a []int, lo, hi int) int { return i + 1 } +// 解法二 func wiggleSort_(nums []int) { if len(nums) < 2 { return diff --git a/Algorithms/0725. Split Linked List in Parts/725. Split Linked List in Parts_test.go b/Algorithms/0725. Split Linked List in Parts/725. Split Linked List in Parts_test.go index a27c893f..433086fa 100644 --- a/Algorithms/0725. Split Linked List in Parts/725. Split Linked List in Parts_test.go +++ b/Algorithms/0725. Split Linked List in Parts/725. Split Linked List in Parts_test.go @@ -47,30 +47,30 @@ func Test_Problem725(t *testing.T) { ans725{[]int{}}, }, - // question725{ - // para725{[]int{1, 2, 3, 2, 3, 2, 3, 2}, 0}, - // ans725{[]int{1, 2, 3, 2, 3, 2, 3, 2}}, - // }, + question725{ + para725{[]int{1, 2, 3, 2, 3, 2, 3, 2}, 0}, + ans725{[]int{1, 2, 3, 2, 3, 2, 3, 2}}, + }, - // question725{ - // para725{[]int{1, 2, 3, 4, 5}, 5}, - // ans725{[]int{1, 2, 3, 4}}, - // }, + question725{ + para725{[]int{1, 2, 3, 4, 5}, 5}, + ans725{[]int{1, 2, 3, 4}}, + }, - // question725{ - // para725{[]int{}, 5}, - // ans725{[]int{}}, - // }, + question725{ + para725{[]int{}, 5}, + ans725{[]int{}}, + }, - // question725{ - // para725{[]int{1, 2, 3, 4, 5}, 10}, - // ans725{[]int{1, 2, 3, 4, 5}}, - // }, + question725{ + para725{[]int{1, 2, 3, 4, 5}, 10}, + ans725{[]int{1, 2, 3, 4, 5}}, + }, - // question725{ - // para725{[]int{1}, 1}, - // ans725{[]int{}}, - // }, + question725{ + para725{[]int{1}, 1}, + ans725{[]int{}}, + }, } fmt.Printf("------------------------Leetcode Problem 725------------------------\n") diff --git a/Algorithms/0977. Squares of a Sorted Array/977. Squares of a Sorted Array.go b/Algorithms/0977. Squares of a Sorted Array/977. Squares of a Sorted Array.go index fef98c97..215fee71 100644 --- a/Algorithms/0977. Squares of a Sorted Array/977. Squares of a Sorted Array.go +++ b/Algorithms/0977. Squares of a Sorted Array/977. Squares of a Sorted Array.go @@ -2,6 +2,7 @@ package leetcode import "sort" +// 解法一 func sortedSquares(A []int) []int { ans := make([]int, len(A)) for i, k, j := 0, len(A)-1, len(ans)-1; i <= j; k-- { @@ -16,6 +17,7 @@ func sortedSquares(A []int) []int { return ans } +// 解法二 func sortedSquares_(A []int) []int { for i, value := range A { A[i] = value * value diff --git a/Algorithms/1021. Remove Outermost Parentheses/1021. Remove Outermost Parentheses.go b/Algorithms/1021. Remove Outermost Parentheses/1021. Remove Outermost Parentheses.go index 919904ec..76ea277a 100644 --- a/Algorithms/1021. Remove Outermost Parentheses/1021. Remove Outermost Parentheses.go +++ b/Algorithms/1021. Remove Outermost Parentheses/1021. Remove Outermost Parentheses.go @@ -1,7 +1,7 @@ package leetcode +// 解法一 func removeOuterParentheses(S string) string { - now, current, ans := 0, "", "" for _, char := range S { if string(char) == "(" { @@ -18,6 +18,7 @@ func removeOuterParentheses(S string) string { return ans } +// 解法二 func removeOuterParentheses_(S string) string { stack, res, counter := []byte{}, "", 0 for i := 0; i < len(S); i++ {