代码
class Solution { public int dominantIndex(int[] nums) { //数组的尺寸 int size = nums.length; //特判 if(size == 1) return 0; //一次遍历,获取最大值 ,次最大值 int max = 0, secondMax =0, maxIndex = 0; for(int i = 0; i < size; i++ ) { if( nums[i] >= max) { //大于最大值,更新最大值,最大值坐标,次最大值 secondMax = max; max = nums[i]; maxIndex = i; } else if( nums[i] > secondMax) { //大于次最大值,更新次最大值 secondMax = nums[i]; } } //次最大值为0,返回maxIndex //最大值的大于等于次最大值的二倍返回maxIndex,否则-1 return secondMax == 0 ? maxIndex : ( (max / secondMax) >= 2 ? maxIndex : -1 ); }}