为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode628. 三个数的最大乘积 | Maximum Product of Three Numbers

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10472942.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Given an integer array, find three numbers whose product is maximum and output the maximum product.

Example 1:

Input: [1,2,3]
Output: 6 

Example 2:

Input: [1,2,3,4]
Output: 24 

Note:

  1. The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
  2. Multiplication of any three numbers in the input won't exceed the range of 32-bit signed integer.

给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积。

示例 1:

输入: [1,2,3]
输出: 6

示例 2:

输入: [1,2,3,4]
输出: 24

注意:

  1. 给定的整型数组长度范围是[3,104],数组中所有的元素范围是[-1000, 1000]。
  2. 输入的数组中任意三个数的乘积不会超出32位有符号整数的范围。

136ms
 1 class Solution {
 2     func maximumProduct(_ nums: [Int]) -> Int {
 3         if(nums.count == 3){ return nums[0] * nums[1] * nums[2] }
 4         let sorted = nums.sorted(by: >)
 5         if(sorted[sorted.count - 1] >= 0 || sorted[0] < 0){
 6             return sorted[0] * sorted[1] * sorted[2]
 7         }
 8         //只有一个负数的情况,取三个正数
 9         if(sorted[sorted.count - 1] < 0 && sorted[sorted.count - 2] > 0){
10             return sorted[0] * sorted[1] * sorted[2]
11         }
12         //有多个负数情况,比较
13         let neg = sorted[sorted.count - 2] * sorted[sorted.count - 1]
14         let pos = sorted[1] * sorted[2]
15         return sorted[0] * max(neg, pos)
16     }
17 }

180ms

 1 class Solution {
 2     func maximumProduct(_ nums: [Int]) -> Int {
 3         var nums = nums
 4         nums.sort { $0 > $1 }
 5         
 6         let count = nums.count
 7         return max(nums[0] * nums[1] * nums[2],
 8                    nums[0] * nums[count - 1] * nums[count - 2])
 9     }
10 }

252ms

 1 class Solution {
 2     func maximumProduct(_ nums: [Int]) -> Int {
 3     if nums.count < 3 {
 4         return 0
 5     }
 6     var numsTemp : [Int] = nums.sorted()
 7     if numsTemp[1] > 0 {
 8         return numsTemp.last! * numsTemp[numsTemp.count - 2] * numsTemp[numsTemp.count - 3]
 9     } else {
10         if numsTemp.last! <= 0 {
11             return numsTemp.last! * numsTemp[numsTemp.count - 2] * numsTemp[numsTemp.count - 3]
12         } else {
13             return numsTemp.last! * numsTemp[numsTemp.count - 2] * numsTemp[numsTemp.count - 3] > numsTemp.first! * numsTemp[1] * numsTemp.last! ? numsTemp.last! * numsTemp[numsTemp.count - 2] * numsTemp[numsTemp.count - 3] : numsTemp.first! * numsTemp[1] * numsTemp.last!
14         }
15      }
16    }
17 }

Runtime: 272 ms

Memory Usage: 19 MB
 1 class Solution {
 2     func maximumProduct(_ nums: [Int]) -> Int {
 3         var mx1:Int = Int.min
 4         var mx2:Int = Int.min
 5         var mx3:Int = Int.min
 6         var mn1:Int = Int.max
 7         var mn2:Int = Int.max
 8         for num in nums
 9         {
10             if num > mx1
11             {
12                 mx3 = mx2
13                 mx2 = mx1
14                 mx1 = num
15             }
16             else if num > mx2
17             {
18                 mx3 = mx2
19                 mx2 = num
20             }
21             else if num > mx3
22             {
23                 mx3 = num
24             }
25             if num < mn1
26             {
27                 mn2 = mn1
28                 mn1 = num
29             }
30             else if num < mn2
31             {
32                 mn2 = num
33             }            
34         }
35         return max(mx1 * mx2 * mx3, mx1 * mn1 * mn2)
36     }
37 }

320ms

 1 class Solution {
 2     func maximumProduct(_ nums: [Int]) -> Int {
 3         if nums.count < 3 { return Int.min }
 4         var max1 = Int.min
 5         var max2 = max1
 6         var max3 = max1
 7         var min1 = Int.max
 8         var min2 = Int.max
 9         
10         nums.forEach {
11             if $0 >= max1 {
12                 max3 = max2
13                 max2 = max1
14                 max1 = $0
15             } else if $0 >= max2 {
16                 max3 = max2
17                 max2 = $0
18             } else if $0 > max3 {
19                 max3 = $0
20             }
21             
22             if $0 <= min1 {
23                 min2 = min1
24                 min1 = $0
25             } else if $0 <= min2 {
26                 min2 = $0
27             }
28         }
29             let val1 = max1 * max2 * max3
30             let val2 = min1 * min2 * max1
31         
32             return max(val1, val2)
33     }   
34 }

388ms

 1 class Solution {
 2     func maximumProduct(_ nums: [Int]) -> Int {
 3         
 4         let sorted = nums.sorted()
 5         
 6         if sorted[sorted.count - 1] == 0 {
 7             return 0
 8         } else if sorted[sorted.count - 1] < 0 {
 9             return sorted[0] * sorted[1] * sorted[sorted.count - 1]
10         } else if sorted[0] >= 0 {
11             return sorted[sorted.count - 1] * sorted[sorted.count - 2] * sorted[sorted.count - 3]
12         } else {
13             return max(sorted[sorted.count - 1] * sorted[sorted.count - 2] * sorted[sorted.count - 3],
14                       sorted[0] * sorted[1] * sorted[sorted.count - 1])
15         }
16     }
17 }

392ms

1 class Solution {
2     func maximumProduct(_ nums: [Int]) -> Int {
3         let sorted = nums.sorted()
4         return max(sorted[nums.count - 1] * sorted[nums.count - 2] * sorted[nums.count - 3], sorted[nums.count - 1] * sorted[0] * sorted[1])
5     }
6 }

404ms

 1 class Solution {
 2     func maximumProduct(_ nums: [Int]) -> Int {
 3         if nums.count == 3 {
 4             return nums.reduce(1, *)
 5         }
 6         var sort = nums.sorted(by: >)
 7         let one = sort[0] * sort[1] * sort[2]
 8         let two = sort[0] * sort[nums.count - 1] * sort[nums.count - 2]
 9         
10         return max(one, two)
11     }
12 }

440ms

 1 class Solution {
 2     func maximumProduct(_ nums: [Int]) -> Int {
 3         let sortGreatestToLeast: [Int] = nums.sorted(by: >)
 4         let greatestThreeNums: [Int] = Array(sortGreatestToLeast.prefix(3))
 5         let leastTwoNums: [Int] = Array(sortGreatestToLeast.suffix(2))
 6         let productFromGreatest: Int = greatestThreeNums.reduce(1, { x, y in x * y })
 7         let productFromBoth: Int = leastTwoNums.reduce(1, { x, y in x * y }) * greatestThreeNums.first!
 8         return productFromGreatest > productFromBoth ? productFromGreatest : productFromBoth
 9     }
10 }

 

 

posted @ 2019-03-04 20:29  为敢技术  阅读(366)  评论(0编辑  收藏  举报