package main
import (
"fmt"
"sort"
)
func main() {
var slice1 = make([]int, 0, 5)
fmt.Println("please input the No.1 array: ")
for i := 1; true; i++ {
var inputNum int
fmt.Printf("No.%d : ", i)
_, err := fmt.Scanf("%d", &inputNum)
if nil == err {
slice1 = append(slice1, inputNum)
} else {
break
}
}
printSlice(slice1)
fmt.Println(threeSum(slice1))
}
func printSlice(x []int){
fmt.Printf("slice=%v len=%d cap=%d \n",x,len(x),cap(x))
}
func threeSum(nums []int) [][]int {
totalLengh := len(nums)
sort.Slice(nums, func(i, j int) bool {
return nums[i] < nums[j]
})
printSlice(nums)
var ans [][]int
for first := 0; first < totalLengh; first++ {
if (first > 0) && (nums[first] == nums[first - 1]) {
continue
}
third := totalLengh - 1
target := -nums[first]
for second := first + 1; second < totalLengh; second++ {
if (second > first + 1) && (nums[second] == nums[second - 1]) {
continue
}
for ; (second < third) && (nums[second] + nums[third] > target); {
third--
}
if second == third {
break
}
if nums[second] + nums[third] == target {
var tmp []int
tmp = append(tmp, nums[first], nums[second], nums[third])
ans = append(ans, tmp)
}
}
}
return ans
}