This is a creation in Article, where the information may have evolved or changed.
Title: Given An array S of n integers, is there elements a, B, C, and D in S such that A + B + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note:the solution set must not contain duplicate quadruplets.
Translation: the array s,s given n integers has elements a,b,c and D, which makes + B + c + d = target? Find all the unique four tuples in the array, giving the sum of the targets.
Note: The solution set cannot contain duplicate four-tuple.
Idea: The idea and 3sum are the same, so don't repeat it.
Directly on the code:
Golang:
Package main import ("FMT" "sort") Func main () {nums: = []int{-3,-2,-1, 0, 0, 1, 2, 3} FMT.P Rintln (foursum (nums, 0))} func foursum (nums []int, Target int) [][]int {sort]. Ints (nums) Arrlen: = Len (nums) Result: = Make ([][]int, 0, 0) Temp: = Make ([]int, 4, 4) var sum int For I: = 0; i < arrLen-3; i++ {If i > 0 && nums[i] = = Nums[i-1] {continue} for J: = i + 1; j < arrLen-2; If j>i+1&&nums[j] = = Nums[j-1] {Continue} low, High: = J+1, arrLen-1 For low < high {sum = Nums[i] + nums[j] + Nums[low] + Nums[high] if sum = = Targ ET {temp = []int{nums[i], nums[j], Nums[low], Nums[high]} result = Append (Resul T, temp) for low < high && nums[low] = = Nums[low+1] {low++ } For low < high && Nums[high] = = Nums[high-1] {high--} low++ high--} else if sum > target {high-- } else {low++}}}}return result
}