Skip to content

Instantly share code, notes, and snippets.

@BaReinhard
Created February 21, 2019 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BaReinhard/b529f2a81ea6534c553ebc42ef7ce66d to your computer and use it in GitHub Desktop.
Save BaReinhard/b529f2a81ea6534c553ebc42ef7ce66d to your computer and use it in GitHub Desktop.
func threeSum(nums []int) [][]int {
sort.Ints(nums[:])
used:= map[string]bool{}
if !(len(nums)>2){
fmt.Printf("Not enough integers in array")
return [][]int{}
}
firstNum:= nums[0]
lastNum:= nums[len(nums)-1]
// Check that the sorted list to see if its a long array of 0's
if firstNum+nums[1]+ lastNum == 0 && firstNum == lastNum{
return [][]int{[]int{0,0,0}}
}
solutions:= [][]int{}
for i := 0; i < len(nums)-2; i++ {
for a:= i+1 ; a<len(nums)-1;a++{
for s:=a+1; s<len(nums);s++{
num1:= nums[i]
num2:= nums[a]
num3:= nums[s]
sum:= num1 + num2 + num3
// if the sorted list finds a sum greater than 0 we can stop this loop
if sum > 0{
break
}
if sum == 0{
arr:= []int{num1,num2,num3}
strRep:= fmt.Sprint(arr)
if !used[strRep]{
used[strRep] = true
solutions = append(solutions,arr)
}
}
}
}
}
return solutions
}
func returnOrderedNumbers(arr []int) string{
sort.Ints(arr[:])
return fmt.Sprint(arr)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment