更新注释

This commit is contained in:
YDZ
2019-06-11 13:31:17 +08:00
parent eea1878fca
commit a2e87d5cc3
18 changed files with 56 additions and 27 deletions

View File

@@ -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++ {

View File

@@ -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

View File

@@ -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

View File

@@ -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)
}

View File

@@ -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

View File

@@ -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)

View File

@@ -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)

View File

@@ -24,4 +24,4 @@ Output: -1->0->3->4->5
## 解题思路
这道题只能用归并排序才能符合要求。归并排序需要的 2 个操作在其他题目已经出现过了,取中间点第 876 题,合并 2 个有序链表第 21 题。
这道题只能用归并排序才能符合要求。归并排序需要的 2 个操作在其他题目已经出现过了,取中间点第 876 题,合并 2 个有序链表第 21 题。

View File

@@ -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 {

View File

@@ -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++ {

View File

@@ -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

View File

@@ -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

View File

@@ -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-- {

View File

@@ -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

View File

@@ -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

View File

@@ -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")

View File

@@ -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

View File

@@ -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++ {