leetcode学习记录
leetcode学习记录三十六
至少是其他数字两倍的最大值
难度:简单
在一个给定的数组nums中,总是存在一个最大元素 。
查找数组中的最大元素是否至少是数组中每个其他数字的两倍。
如果是,则返回最大元素的索引,否则返回-1。
示例 1:
输入: nums = [3, 6, 1, 0]
输出: 1
解释: 6是最大的整数, 对于数组中的其他整数,
6大于数组中其他元素的两倍。6的索引是1, 所以我们返回1.
示例 2:
输入: nums = [1, 2, 3, 4]
输出: -1
解释: 4没有超过3的两倍大, 所以我们返回 -1.
提示:
nums 的长度范围在[1, 50].
每个 nums[i] 的整数范围在 [0, 100].
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/largest-number-at-least-twice-of-others
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路
1.本题思路很简单,直接暴力法也可以,有一技巧是可以在第一遍遍历时找到第一大和第二大的,如果第二大的两倍大于第一大的,则存在
代码
//方法一
class Solution {
public int dominantIndex(int[] nums) {
int max = Integer.MIN_VALUE;
int index = 0;
int count = 0;
for(int i = 0; i < nums.length; i++){
if(nums[i] > max){
max = nums[i];
index = i;
}
}
for(int i = 0; i < nums.length; i++){
if(max < nums[i]*2){
count++;
}
if(count > 1){
return -1;
}
}
return index;
}
}
//方法二
class Solution {
public int dominantIndex(int[] nums) {
int max = 0;
int index = 0;
int second = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] > max) {
second = max;
max = nums[i];
index = i;
}
else {
if (nums[i] > second) {
second = nums[i];
}
}
}
return max >= (second * 2) ? index : -1;
}
}