Largest Number At Least Twice of Others
找最大的数和他的index, 然后和其他数比较一下是不是有两倍.
class Solution {
public int dominantIndex(int[] nums) {
int max = Integer.MIN_VALUE;
int maxIndex = -1;
for(int i = 0; i < nums.length; i++) {
if(nums[i] > max) {
max = nums[i];
maxIndex = i;
}
}
for(int i = 0; i < nums.length; i++) {
if(maxIndex == i)
continue;
if(nums[i] * 2 > max) {
return -1;
}
}
return maxIndex;
}
}