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语言描述队列的实现及操作(链表实现)

    // 队列的单链表实现 // 头节点:哨兵作用,不存放数据,用来初始化队列时使队头队尾指向的地方 // 首节点:头节点后第一个节点,存放数据 #include<stdio.h> #incl ...

  2. python 函数基础 定义

    一.函数介绍 1.为什么要有函数? 没有函数的代码组织结构不清晰,可读性差. 代码冗余 管理维护难度大,扩展性 2.什么是函数? 具备某一个功能的工具就是程序中的函数. 事先准备工具的过程就是:函数的 ...

  3. Python 自学 之 String 常见操作

    这是在Python 3.5.3版本下测试的.# Author Taylor_Manitoname ="my name is alex"#capitalized 大写的print(& ...

  4. New UWP Community Toolkit - RadialProgressBar

    概述 UWP Community Toolkit  中有一个圆形的进度条控件 - RadialProgressBar,本篇我们结合代码详细讲解  RadialProgressBar 的实现. Radi ...

  5. 福州大学软工1715|W班-启航

    新的一学期即将开启,而在仅剩的几天的时间内,我将为接下来的软工实践助教事宜忙碌起来.要学习的东西很多,要关注的东西也很多. 虽然我现在还在茫然阶段,虽然我对<构建之法>还不太熟悉,但是,我 ...

  6. DEVC使用问题集锦

    一.DEVC++编译出现"Id return 1 exit status" 这是初学者刚用DEVC经常碰到问题,一般有如下解决方法: 1.首先检查下是否有c的exe程序开着,若开着 ...

  7. UserControl 用户定义组件

    <pages> <namespaces> <add namespace="System.Web.Optimization" /> </na ...

  8. 201621123060 《Java程序设计》第六周学习总结

    1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰 ...

  9. python 之反射

    通过字符串的形式导入模块 通过字符串的形式,去模块中寻找制定的函数,并执行getattr(模块名,函数名,默认值) 通过字符串的形式,去模块中设置东西setattr(模块名,函数名/变量名,lambd ...

  10. 【iOS】swift-文字宽度的计算

    如图所示,需要sectionView的标题宽度可以动态变化 举例说明: 只需在tableView的代理方法 func tableView(tableView: UITableView, viewFor ...