LeetCode题:至少是其他数字两倍的最大数dominantIndex
def dominantIndex(self, nums):
"""
:type nums: List[int]
:rtype: int
* 思路:
* 边比较边找出最大值
* 找出最大值max和第二大的值temp
"""
max = nums[0]
temp = -10
index = -1
swap = 1
if len(nums) < 1:
return -1
if len(nums) == 1:
return 0
for i in range(len(nums)):
if nums[i] > max:
temp = max
max = nums[i]
swap = 0
index = i
elif nums[i] >= temp and nums[i] < max:
temp = nums[i]
if max/2 >= temp:
if swap:
return 0
else:
return index
else:
return -1