★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(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
nums
will have a length in the range [1, 50]
.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.
提示:
nums
的長度範圍在[1, 50]
.nums[i]
的整數範圍在 [0, 99]
.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 }