Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created August 21, 2017 07:16
Show Gist options
  • Save luojiyin1987/4828a5e6428f047c7f1d3ab9626d2932 to your computer and use it in GitHub Desktop.
Save luojiyin1987/4828a5e6428f047c7f1d3ab9626d2932 to your computer and use it in GitHub Desktop.
Maximum Product of Three Numbers
func maximumProduct(nums []int) int {
sort.Ints(nums)
l := len(nums)
if nums[0] <0 && nums[l-1] <=0 {
return nums[l-3] * nums[l-2] * nums[l-1]
}
if nums[0]>= 0 && nums[l-1] >0 {
return nums[l-3] * nums[l-2] * nums[l-1]
}
if nums[0] <0 && nums[l-1] >0 {
temp1 := nums[0] *nums[1] * nums[l-1]
temp2 := nums[l-3] *nums[l-2] * nums[l-1]
if temp1 > temp2 {
return temp1
} else {
return temp2
}
}
return 0
}
//-------------------------------------------
func maximumProduct(nums []int) int {
sort.Ints(nums)
l := len(nums)
temp1 := nums[0] * nums[1] * nums[l-1]
temp2 := nums[l-3] *nums[l-2] *nums[l-1]
if temp1 > temp2 {
return temp1
}else{
return temp2
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment