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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-ggsdetem-me.html 
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

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

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:dom

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:spa

  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中,老是存在一个最大元素 。code

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

若是是,则返回最大元素的索引,不然返回-1。blog

示例 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 }
相关文章
相关标签/搜索