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

[Swift]LeetCode747. 至少是其他数字两倍的最大数 | Largest Number At Least Twice of Others

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

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

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

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

In a given integer array nums, there is always exactly one largest element.

Find whether the largest element in the array is at least twice as much as every other number in the array.

If it is, return the index of the largest element, otherwise return -1.

Example 1:

Input: nums = [3, 6, 1, 0]
Output: 1
Explanation: 6 is the largest integer, and for every other number in the array x,
6 is more than twice as big as x.  The index of value 6 is 1, so we return 1. 

Example 2:

Input: nums = [1, 2, 3, 4]
Output: -1
Explanation: 4 isn't at least as big as twice the value of 3, so we return -1. 

Note:

  1. nums will have a length in the range [1, 50].
  2. Every nums[i] will be an integer in the range [0, 99].

在一个给定的数组nums中,总是存在一个最大元素 。

查找数组中的最大元素是否至少是数组中每个其他数字的两倍。

如果是,则返回最大元素的索引,否则返回-1。

示例 1:

输入: nums = [3, 6, 1, 0]
输出: 1
解释: 6是最大的整数, 对于数组中的其他整数,
6大于数组中其他元素的两倍。6的索引是1, 所以我们返回1. 

示例 2:

输入: nums = [1, 2, 3, 4]
输出: -1
解释: 4没有超过3的两倍大, 所以我们返回 -1. 

提示:

  1. nums 的长度范围在[1, 50].
  2. 每个 nums[i] 的整数范围在 [0, 99].

Runtime: 12 ms
Memory Usage: 18.7 MB
 1 class Solution {
 2     func dominantIndex(_ nums: [Int]) -> Int {
 3         var mx:Int = Int.min
 4         var mxId:Int = 0
 5         for i in 0..<nums.count
 6         {
 7             if mx < nums[i]
 8             {
 9                 mx = nums[i]
10                 mxId = i
11             }
12         }
13         for num in nums
14         {
15             if mx != num && mx - num < num
16             {
17                 return -1
18             }
19         }
20         return mxId
21     }
22 }

12ms

1 class Solution {
2     func dominantIndex(_ nums: [Int]) -> Int {
3     let maximumNumber = nums.max()!
4     let index = nums.firstIndex(of: maximumNumber)!
5      let _nums = nums.filter{$0 * 2 > maximumNumber}
6        return _nums.count > 1 ? -1 : index        
7     }
8 }

16ms

 1 class Solution {
 2     func dominantIndex(_ nums: [Int]) -> Int {
 3         guard nums.count > 1 else {
 4             return 0
 5         }
 6         
 7        let ar = nums.enumerated().sorted{$0.1 > $1.1}
 8         if ar[0].1 >= 2*ar[1].1 {
 9             return ar[0].0
10         }
11         return -1
12     }
13 }

16ms

 1 class Solution {
 2     func dominantIndex(_ nums: [Int]) -> Int {
 3         if nums.count == 1 {
 4             return 0
 5         }
 6         
 7         var index: Int = 0
 8         var maxValue: Int = 0
 9         var secondValue: Int = 0
10     
11         for i in 0...nums.count - 1{            
12             if nums[i] > maxValue{
13                 secondValue = maxValue
14                 maxValue = nums[i]
15                 index = i
16             }else if nums[i] > secondValue{
17                 secondValue = nums[i]
18             }           
19         }
20         
21         return maxValue >= (secondValue * 2) ? index : -1
22     }
23 }

20ms

 1 class Solution {
 2     
 3     var greatestNumber = (index: -1, value: -1)
 4     var secondGreatestNumber = (index: -1, value: -1)
 5     var currentNumber = (index: -1, value: -1)
 6     
 7     func dominantIndex(_ nums: [Int]) -> Int {
 8 
 9         findTwoGreatestNumbers(in: nums)
10         if greatestNumber.value >= secondGreatestNumber.value * 2 {
11             return greatestNumber.index
12         } else {
13             return -1
14         }
15     }
16     
17     private func findTwoGreatestNumbers(in nums: [Int]) {
18         for (index, value) in nums.enumerated() {
19             currentNumber = (index,value)
20             if currentNumber.value > greatestNumber.value {
21                 secondGreatestNumber = greatestNumber
22                 greatestNumber = currentNumber
23             } else if currentNumber.value > secondGreatestNumber.value {
24                 secondGreatestNumber = currentNumber
25             }
26         }
27     }
28 }

24ms

 1 class Solution {
 2     func dominantIndex(_ nums: [Int]) -> Int {
 3         guard !nums.isEmpty else {
 4             return -1
 5         }
 6         
 7         var maxIndex = 0
 8         for index in 1..<nums.count {
 9             let num = nums[index]
10             if num > nums[maxIndex] {
11                 maxIndex = index
12             }
13         }
14         
15         let maxNum = nums[maxIndex]
16         for num in nums where num != 0 && num != maxNum {
17             if maxNum / num < 2 {
18                 return -1
19             }
20         }
21         
22         return maxIndex
23     }
24 }

28ms

1 class Solution {
2     func dominantIndex(_ nums: [Int]) -> Int {
3     guard let largestNumber = nums.sorted(by: >).first else { return -1 }
4     let dominateNums = nums.filter({ $0 != largestNumber && largestNumber < ($0 * 2) })
5     return dominateNums.isEmpty ? nums.index(of: largestNumber)! : -1
6    }
7 }

 

posted @ 2019-03-13 19:36  为敢技术  阅读(237)  评论(0编辑  收藏  举报