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.

Example 1:

Input: nums = [3, 6, 1, 0]
Output: 1
Explanation: 6 is the largest integer, and for every other number in the array x,
6 is more than twice as big as x. The index of value 6 is 1, so we return 1.

Example 2:

Input: nums = [1, 2, 3, 4]
Output: -1
Explanation: 4 isn't at least as big as twice the value of 3, so we return -1.

Note:

  1. nums will have a length in the range [1, 50].
  2. Every nums[i] will be an integer in the range [0, 99].

这道题让我们找一个至少是其他数字两倍的最大数字,那么我们想,首先明确的一点是这个要求的数字一定是数组中的最大数字,因为其是其他所有的数字的至少两倍。然后就是,如果该数字是数组中第二大的数字至少两倍的话,那么它一定是其他所有数字的至少两倍,所以我们可以遍历一次数组分别求出最大数字和第二大数字,然后判断一下最大数字是否是第二大数字的两倍即可,注意这里我们判断两倍的方法并不是直接相除,为了避免除以零的情况,我们采用减法,参见代码如下:

解法一:

class Solution {
public:
int dominantIndex(vector<int>& nums) {
int mx = INT_MIN, secondMx = INT_MIN, mxId = ;
for (int i = ; i < nums.size(); ++i) {
if (nums[i] > mx) {
secondMx = mx;
mx = nums[i];
mxId = i;
} else if (nums[i] > secondMx) {
secondMx = nums[i];
}
}
return (mx - secondMx >= secondMx) ? mxId : -;
}
};

当然我们也可以使用更straightforward的方法,首先遍历一遍数组找出最大数字,然后再遍历一遍数组,验证这个数字是否是其他数字的至少两倍,参见代码如下:

解法二:

class Solution {
public:
int dominantIndex(vector<int>& nums) {
int mx = INT_MIN, mxId = ;
for (int i = ; i < nums.size(); ++i) {
if (mx < nums[i]) {
mx = nums[i];
mxId = i;
}
}
for (int num : nums) {
if (mx != num && mx - num < num) return -;
}
return mxId;
}
};

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Largest Number At Least Twice of Others 至少是其他数字两倍的最大数的更多相关文章

  1. Leetcode747.Largest Number At Least Twice of Others至少是其他数字两倍的最大数

    在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 1: 输入: nums = [3, ...

  2. LeetCode:至少是其他数字两倍的最大数【747】

    LeetCode:至少是其他数字两倍的最大数[747] 题目描述 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素 ...

  3. [Swift]LeetCode747. 至少是其他数字两倍的最大数 | Largest Number At Least Twice of Others

    In a given integer array nums, there is always exactly one largest element. Find whether the largest ...

  4. [LC]747题 Largest Number At Least Twice of Others (至少是其他数字两倍的最大数)

    ①中文题目 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 1: 输入: nums ...

  5. Java实现 LeetCode 747 至少是其他数字两倍的最大数(暴力)

    747. 至少是其他数字两倍的最大数 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 ...

  6. C#LeetCode刷题之#747-至少是其他数字两倍的最大数( Largest Number At Least Twice of Others)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3746 访问. 在一个给定的数组nums中,总是存在一个最大元素 ...

  7. [LeetCode] Largest Number 最大组合数

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  8. Leetcode Largest Number c++ solution

    Total Accepted: 16020 Total Submissions: 103330     Given a list of non negative integers, arrange t ...

  9. LeetCode: Largest Number 解题报告 以及Comparator, CompareTo 应用

    Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...

随机推荐

  1. C++迭代器的使用和操作总结

    迭代器是一种检查容器内元素并遍历元素的数据类型.C++更趋向于使用迭代器而不是下标操作,因为标准库为每一种标准容器(如vector)定义了一种迭代器类型,而只用少数容器(如vector)支持下标操作访 ...

  2. JSON字符串转为JSON对象

    在数据传输流程中,json是以文本,即字符串的形式传递的,而JS操作的是对象,所以,JSON对象(js对象)和JSON字符串之间的相互转换是关键. JSON可以有两种格式,一种是对象格式的,另一种是数 ...

  3. Android 源代码结构

    简介 在使用Andriod SDK进行应用程序开发的时候,我们需要对源代码进行调试,有可能需要进入到某个Android API函数内部进行跟踪调试.但是,如果目标版本的SDK没有关联对应版本的源代码的 ...

  4. 生产者/消费者问题的多种Java实现方式

    实质上,很多后台服务程序并发控制的基本原理都可以归纳为生产者/消费者模式,而这是恰恰是在本科操作系统课堂上老师反复讲解,而我们却视而不见不以为然的.在博文<一种面向作业流(工作流)的轻量级可复用 ...

  5. (译文)学习ES6非常棒的特性——Async / Await函数

    try/catch 在使用Async/Await前,我们可能这样写: const main = (paramsA, paramsB, paramsC, done) => { funcA(para ...

  6. java————面向对象概念

    面向对象 OO:面向对象 OOP:面向对象编程 OOA:面向对象分析 OOD:面向对象设计 面向对象的特征 继承,封装,多态 什么是对象? 对象是存在的具体实体,具有明确定义的特征和行为. 万物皆对象 ...

  7. Linux 磁盘和文件管理系统 文件打包解压备份 VIM、VI编辑器

  8. TensorFlow-谷歌深度学习库 手把手教你如何使用谷歌深度学习云平台

    自己的电脑跑cnn, rnn太慢? 还在为自己电脑没有好的gpu而苦恼? 程序一跑一俩天连睡觉也要开着电脑训练? 如果你有这些烦恼何不考虑考虑使用谷歌的云平台呢?注册之后即送300美元噢-下面我就来介 ...

  9. JAVA_SE基础——70.Math类

    package cn.itcast.other; /*  Math 数学类, 主要是提供了很多的数学公式.    abs(double a)  获取绝对值  ceil(double a)  向上取整 ...

  10. Ubuntu命令行连接WPA/WPA2无线网线

    一,连接无加密无线网络zhang:sudo ip link set wlan0 up sudo iw dev wlan0 connect zhangsudo dhclient wlan0 二,连接WP ...