leetCode刷题记录之747Largest Number At Least Twice of Others

题目原文

In a given integer array nums, there is always exactly one largest element.
Find whether the largest element in the array is at least twice as much as every other number in the array.
If it is, return the index of the largest element, otherwise return -1.

题目解释

给定一个整数数组,它一定有一个最大值,看看这个最大值是否至少是其它值的2倍,如果不是返回-1,否则返回最大值对应的下标

我的代码

/**
 * @param {number[]} nums
 * @return {number}
 */
var dominantIndex = function(nums) {
    var max = Math.max.apply(null,nums); //这里求最大值
    for(var index = 0; index <= nums.length; index++)
    {	// 这里要排除自己
        if(max < nums[index] * 2 && max != nums[index]) return -1;
    }    
    for(var index = 0; index <= nums.length; index++)
    {
        if(max == nums[index]) return index;
    } 
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值