You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

65 lines
1.7 KiB

package main
import "fmt"
func main() {
nums := []int{-1, 0, 1, 2, -1, -4}
fmt.Printf("打印结构体指针数组1:%v \n", threeSum(nums))
/*nums = []int{0,1,1}
fmt.Printf("打印结构体指针数组1:%v \n", threeSum(nums))
nums = []int{0,0,0}
fmt.Printf("打印结构体指针数组1:%v \n", threeSum(nums))
nums = []int{3,0,-2,-1,1,2}
fmt.Printf("打印结构体指针数组1:%v \n", threeSum(nums))
nums = []int{-1,0,1,2,-1,-4,-2,-3,3,0,4}
fmt.Printf("打印结构体指针数组1:%v \n", threeSum(nums))*/
}
/**
给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != j、i != k 且 j != k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请
你返回所有和为 0 且不重复的三元组。
注意:答案中不可以包含重复的三元组。
*/
func threeSum(nums []int) [][]int {
var arr [][]int
l := len(nums)
if l < 3 {
return arr
}
if l == 3 && nums[0]+nums[1]+nums[2] == 0 {
return append(arr, []int{nums[0], nums[1], nums[2]})
}
a:
for i := 0; i < l; i++ {
for j := 0; j < l && j != i; j++ {
for k := 0; k < l && k != j; k++ {
if nums[i]+nums[j]+nums[k] == 0 {
for _, ints := range arr {
if (ints[0] == i && ints[1] == j && ints[2] == k) ||
(ints[0] == i && ints[2] == j && ints[1] == k) ||
(ints[1] == i && ints[2] == j && ints[0] == k) ||
(ints[1] == i && ints[0] == j && ints[2] == k) ||
(ints[2] == i && ints[0] == j && ints[1] == k) ||
(ints[2] == i && ints[1] == j && ints[0] == k) {
continue a
}
}
arr = append(arr, []int{i, j, k, nums[i], nums[j], nums[k]})
}
}
}
}
arr2 := arr
for i, ints := range arr2 {
arr2[i] = ints[3:]
// TODO
}
return arr2
}