Created
February 21, 2019 19:53
-
-
Save BaReinhard/b529f2a81ea6534c553ebc42ef7ce66d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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