Largest Number At Least Twice of Others
找最大的数和他的index, 然后和其他数比较一下是不是有两倍.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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; } } |