Created
August 21, 2017 07:16
-
-
Save luojiyin1987/4828a5e6428f047c7f1d3ab9626d2932 to your computer and use it in GitHub Desktop.
Maximum Product of Three Numbers
This file contains hidden or 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 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