mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
规范格式
This commit is contained in:
38
leetcode/0015.3Sum/15. 3Sum.go
Normal file
38
leetcode/0015.3Sum/15. 3Sum.go
Normal file
@ -0,0 +1,38 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
func threeSum(nums []int) [][]int {
|
||||
res := [][]int{}
|
||||
counter := map[int]int{}
|
||||
for _, value := range nums {
|
||||
counter[value]++
|
||||
}
|
||||
|
||||
uniqNums := []int{}
|
||||
for key := range counter {
|
||||
uniqNums = append(uniqNums, key)
|
||||
}
|
||||
sort.Ints(uniqNums)
|
||||
|
||||
for i := 0; i < len(uniqNums); i++ {
|
||||
if (uniqNums[i]*3 == 0) && counter[uniqNums[i]] >= 3 {
|
||||
res = append(res, []int{uniqNums[i], uniqNums[i], uniqNums[i]})
|
||||
}
|
||||
for j := i + 1; j < len(uniqNums); j++ {
|
||||
if (uniqNums[i]*2+uniqNums[j] == 0) && counter[uniqNums[i]] > 1 {
|
||||
res = append(res, []int{uniqNums[i], uniqNums[i], uniqNums[j]})
|
||||
}
|
||||
if (uniqNums[j]*2+uniqNums[i] == 0) && counter[uniqNums[j]] > 1 {
|
||||
res = append(res, []int{uniqNums[i], uniqNums[j], uniqNums[j]})
|
||||
}
|
||||
c := 0 - uniqNums[i] - uniqNums[j]
|
||||
if c > uniqNums[j] && counter[c] > 0 {
|
||||
res = append(res, []int{uniqNums[i], uniqNums[j], c})
|
||||
}
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
59
leetcode/0015.3Sum/15. 3Sum_test.go
Normal file
59
leetcode/0015.3Sum/15. 3Sum_test.go
Normal file
@ -0,0 +1,59 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question15 struct {
|
||||
para15
|
||||
ans15
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para15 struct {
|
||||
a []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans15 struct {
|
||||
one [][]int
|
||||
}
|
||||
|
||||
func Test_Problem15(t *testing.T) {
|
||||
|
||||
qs := []question15{
|
||||
|
||||
question15{
|
||||
para15{[]int{0, 0, 0}},
|
||||
ans15{[][]int{[]int{0, 0, 0}}},
|
||||
},
|
||||
|
||||
question15{
|
||||
para15{[]int{-1, 0, 1, 2, -1, -4}},
|
||||
ans15{[][]int{[]int{-1, 0, 1}, []int{-1, -1, 2}}},
|
||||
},
|
||||
|
||||
question15{
|
||||
para15{[]int{-4, -2, -2, -2, 0, 1, 2, 2, 2, 3, 3, 4, 4, 6, 6}},
|
||||
ans15{[][]int{[]int{-4, -2, 6}, []int{-4, 0, 4}, []int{-4, 1, 3}, []int{-4, 2, 2}, []int{-2, -2, 4}, []int{-2, 0, 2}}},
|
||||
},
|
||||
|
||||
question15{
|
||||
para15{[]int{5, -7, 3, -3, 5, -10, 4, 8, -3, -8, -3, -3, -1, -8, 6, 4, -4, 7, 2, -5, -2, -7, -3, 7, 2, 4, -6, 5}},
|
||||
ans15{[][]int{[]int{-10, 2, 8}, []int{-10, 3, 7}, []int{-10, 4, 6}, []int{-10, 5, 5}, []int{-8, 2, 6}, []int{-8, 3, 5}, []int{-8, 4, 4}, []int{-7, -1, 8},
|
||||
[]int{-7, 2, 5}, []int{-7, 3, 4}, []int{-6, -2, 8}, []int{-6, -1, 7}, []int{-6, 2, 4}, []int{-5, -3, 8}, []int{-5, -2, 7}, []int{-5, -1, 6}, []int{-5, 2, 3},
|
||||
[]int{-4, -3, 7}, []int{-4, -2, 6}, []int{-4, -1, 5}, []int{-4, 2, 2}, []int{-3, -3, 6}, []int{-3, -2, 5}, []int{-3, -1, 4}, []int{-2, -1, 3}}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 15------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans15, q.para15
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, threeSum(p.a))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
36
leetcode/0015.3Sum/README.md
Normal file
36
leetcode/0015.3Sum/README.md
Normal file
@ -0,0 +1,36 @@
|
||||
# [15. 3Sum](https://leetcode.com/problems/3sum/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
|
||||
|
||||
Note:
|
||||
|
||||
The solution set must not contain duplicate triplets.
|
||||
|
||||
Example:
|
||||
|
||||
```c
|
||||
Given array nums = [-1, 0, 1, 2, -1, -4],
|
||||
|
||||
A solution set is:
|
||||
[
|
||||
[-1, 0, 1],
|
||||
[-1, -1, 2]
|
||||
]
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个数组,要求在这个数组中找出 3 个数之和为 0 的所有组合。
|
||||
|
||||
## 解题思路
|
||||
|
||||
用 map 提前计算好任意 2 个数字之和,保存起来,可以将时间复杂度降到 O(n^2)。这一题比较麻烦的一点在于,最后输出解的时候,要求输出不重复的解。数组中同一个数字可能出现多次,同一个数字也可能使用多次,但是最后输出解的时候,不能重复。例如 [-1,-1,2] 和 [2, -1, -1]、[-1, 2, -1] 这 3 个解是重复的,即使 -1 可能出现 100 次,每次使用的 -1 的数组下标都是不同的。
|
||||
|
||||
这里就需要去重和排序了。map 记录每个数字出现的次数,然后对 map 的 key 数组进行排序,最后在这个排序以后的数组里面扫,找到另外 2 个数字能和自己组成 0 的组合。
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user