天天看点

Golang Leetcode 747. Largest Number At Least Twice of Others.go思路code

思路

找到最大的和第二大的,如果最大的大于等于第二大的两倍,就找到了

code

func dominantIndex(nums []int) int {
	if len(nums) == 0 {
		return -1
	}
	if len(nums) == 1 {
		return 0
	}
	max, sec := math.MinInt32, math.MinInt32
	p := -1
	for i := 0; i < len(nums); i++ {
		if nums[i] > max {
			sec = max
			max = nums[i]
			p = i
		} else if nums[i] > sec {
			sec = nums[i]
		}
	}
	if max > 2*sec {
		return p
	}
	return -1
}           

复制

更多内容请移步我的repo:https://github.com/anakin/golang-leetcode