diff --git a/双指针/三数之和/main.go b/双指针/三数之和/main.go new file mode 100644 index 0000000..09a0efc --- /dev/null +++ b/双指针/三数之和/main.go @@ -0,0 +1,92 @@ +package main + +import ( + "fmt" + "sort" +) + +func main() { + fmt.Println(threeSum([]int{1, -1, -1, 0})) +} + +func threeSum(nums []int) [][]int { + result := make([][]int, 0) + sort.Ints(nums) + + for i := 0; i < len(nums); i++ { + if i-1 >= 0 { + if nums[i] == nums[i-1] { + continue + } + } + left, right := i+1, len(nums)-1 + for left < right { + num := nums[left] + nums[right] + if num == -nums[i] { + value := make([]int, 0) + value = append(value, nums[left], nums[right], nums[i]) + result = append(result, value) + left++ + right-- + if left-1 > 0 { + for nums[left] == nums[left-1] && left < right { + left++ + } + } + if right+1 < len(nums) { + for nums[right] == nums[right+1] && left < right { + right-- + } + } + + } else if num < -nums[i] { + left++ + } else if num >= -nums[i] { + right-- + } + + } + } + + return result +} + +//func threeSum(nums []int) [][]int { +// result := make([][]int, 0) +// hash := make(map[int][]int) +// result2 := make(map[[3]int]struct{}, 0) +// right := len(nums) - 1 +// for i := 0; i < right+1; i++ { +// hash[nums[i]] = append(hash[nums[i]], i) +// } +// fmt.Println(hash) +// for left := 0; left < len(nums); left++ { +// for right := left + 1; right < len(nums); right++ { +// sum := nums[left] + nums[right] +// z := -sum +// if value, ok := hash[z]; ok { +// for _, v := range value { +// if v == left || v == right { +// continue +// } else { +// num := [3]int{nums[v], nums[left], nums[right]} +// slice := num[:] // 或直接创建新切片:slice := []int{num[0], num[1], num[2]} +// sort.Ints(slice) +// result2[[3]int(slice)] = struct{}{} +// } +// } +// } else { +// continue +// } +// } +// } +// fmt.Println("result2", result2) +// for k, _ := range result2 { +// value := make([]int, 0) +// for _, v := range k { +// value = append(value, v) +// } +// result = append(result, value) +// } +// return result +//} diff --git a/双指针/快速排序/main.go b/双指针/快速排序/main.go new file mode 100644 index 0000000..f45db20 --- /dev/null +++ b/双指针/快速排序/main.go @@ -0,0 +1,32 @@ +package main + +import "fmt" + +func main() { + arr := []int{2, 35, 5, 33, 7, 8, 9, -2} + fmt.Println(partition(arr, 0, len(arr)-1)) + fmt.Println("排序结果:", arr) // 输出: [-2 2 5 7 8 9 33 35] +} + +func quickSort(arr []int, low, high int) { + if low >= high { + return + } + pivotIndex := partition(arr, low, high) + quickSort(arr, low, pivotIndex-1) // 递归左子数组 + quickSort(arr, pivotIndex+1, high) // 递归右子数组 +} + +func partition(arr []int, low, high int) int { + pivot := arr[high] // 选择最后一个元素为基准值 + i := low - 1 // i标记小于基准值的边界 + for j := low; j < high; j++ { + if arr[j] <= pivot { + i++ + arr[i], arr[j] = arr[j], arr[i] // 交换小于基准值的元素到左侧 + } + } + // 将基准值放到正确位置 + arr[i+1], arr[high] = arr[high], arr[i+1] + return i + 1 +} diff --git a/TwoPointers/jieyushui/main.go b/双指针/接雨水/main.go similarity index 100% rename from TwoPointers/jieyushui/main.go rename to 双指针/接雨水/main.go diff --git a/双指针/盛最多的水/main.go b/双指针/盛最多的水/main.go new file mode 100644 index 0000000..db2ef7f --- /dev/null +++ b/双指针/盛最多的水/main.go @@ -0,0 +1,25 @@ +package main + +func main() { + +} +func maxArea(height []int) int { + + left := 0 + right := len(height) - 1 + maxscore := 0 + for left < right { + temp := min(height[left], height[right]) * (right - left) + if temp > maxscore { + maxscore = temp + } + if height[left] < height[right] { + left++ + } else { + right-- + } + + } + + return maxscore +} diff --git a/双指针/移动零/main.go b/双指针/移动零/main.go new file mode 100644 index 0000000..d31813e --- /dev/null +++ b/双指针/移动零/main.go @@ -0,0 +1,18 @@ +package main + +func main() { + moveZeroes([]int{0, 1, 0, 3, 12}) +} +func moveZeroes(nums []int) { + slow := 0 + for slow < len(nums) { + for fast := slow + 1; fast < len(nums); fast++ { + if nums[slow] == 0 { + nums[slow], nums[fast] = nums[fast], nums[slow] + } else { + break + } + } + slow++ + } +} diff --git a/Hash/liangshuzhihe/main.go b/哈希/两数之和/main.go similarity index 100% rename from Hash/liangshuzhihe/main.go rename to 哈希/两数之和/main.go diff --git a/Hash/zimuyicifenzu/main.go b/哈希/字母异次分组/main.go similarity index 100% rename from Hash/zimuyicifenzu/main.go rename to 哈希/字母异次分组/main.go diff --git a/Hash/zuichanglianxuxulie/main.go b/哈希/最长连续序列/main.go similarity index 100% rename from Hash/zuichanglianxuxulie/main.go rename to 哈希/最长连续序列/main.go