在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ In a given integer array 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。 示例 1: 输入: nums = [3, 6, 1, 0] 输出: 1 解释: 6是最大的整数, 对于数组中的其他整数, 6大于数组中其他元素的两倍。6的索引是1, 所以我们返回1. 示例 2: 输入: nums = [1, 2, 3, 4] 输出: -1 解释: 4没有超过3的两倍大, 所以我们返回 -1. 提示:
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 }
|
请发表评论