4Sum Solutions in Go
Number 18
Difficulty Medium
Acceptance 33.7%
Link LeetCode
Other languages C++
Solutions
Go solution by halfrost/LeetCode-Go
package leetcodeimport "sort"func fourSum(nums []int, target 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]*4 == target) && counter[uniqNums[i]] >= 4 {res = append(res, []int{uniqNums[i], uniqNums[i], uniqNums[i], uniqNums[i]})}for j := i + 1; j < len(uniqNums); j++ {if (uniqNums[i]*3+uniqNums[j] == target) && counter[uniqNums[i]] > 2 {res = append(res, []int{uniqNums[i], uniqNums[i], uniqNums[i], uniqNums[j]})}if (uniqNums[j]*3+uniqNums[i] == target) && counter[uniqNums[j]] > 2 {res = append(res, []int{uniqNums[i], uniqNums[j], uniqNums[j], uniqNums[j]})}if (uniqNums[j]*2+uniqNums[i]*2 == target) && counter[uniqNums[j]] > 1 && counter[uniqNums[i]] > 1 {res = append(res, []int{uniqNums[i], uniqNums[i], uniqNums[j], uniqNums[j]})}for k := j + 1; k < len(uniqNums); k++ {if (uniqNums[i]*2+uniqNums[j]+uniqNums[k] == target) && counter[uniqNums[i]] > 1 {res = append(res, []int{uniqNums[i], uniqNums[i], uniqNums[j], uniqNums[k]})}if (uniqNums[j]*2+uniqNums[i]+uniqNums[k] == target) && counter[uniqNums[j]] > 1 {res = append(res, []int{uniqNums[i], uniqNums[j], uniqNums[j], uniqNums[k]})}if (uniqNums[k]*2+uniqNums[i]+uniqNums[j] == target) && counter[uniqNums[k]] > 1 {res = append(res, []int{uniqNums[i], uniqNums[j], uniqNums[k], uniqNums[k]})}c := target - uniqNums[i] - uniqNums[j] - uniqNums[k]if c > uniqNums[k] && counter[c] > 0 {res = append(res, []int{uniqNums[i], uniqNums[j], uniqNums[k], c})}}}}return res}