在一个给定的数组nums中,总是存在一个最大元素 。

查找数组中的最大元素是否至少是数组中每个其他数字的两倍。

如果是,则返回最大元素的索引,否则返回-1。

示例 1:

输入: nums = [3, 6, 1, 0] 输出: 1 解释: 6是最大的整数, 对于数组中的其他整数, 6大于数组中其他元素的两倍。6的索引是1, 所以我们返回1.

示例 2:

输入: nums = [1, 2, 3, 4] 输出: -1 解释: 4没有超过3的两倍大, 所以我们返回 -1.

提示:

  1. nums 的长度范围在[1, 50].
  2. 每个 nums[i] 的整数范围在 [0, 99].
class Solution {
public:
int dominantIndex(vector<int>& nums) {
int MAX = -1;
int res = -1;
int len = nums.size();
for(int i = 0; i < len; i++)
{
if(nums[i] > MAX)
{
MAX = nums[i];
res = i;
}
}
for(int i = 0; i < len; i++)
{
if(res != i && MAX < 2 * nums[i])
return -1;
}
return res;
}
};

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

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

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

  2. Leetcode747至少是其他数字两倍的最大数

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

  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. LeetCode747 至少是其他数字两倍的最大数

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

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

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

  7. 747. Largest Number At Least Twice of Others比所有数字都大两倍的最大数

    [抄题]: In a given integer array nums, there is always exactly one largest element. Find whether the l ...

  8. Java 特定规则排序-LeetCode 179 Largest Number

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

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

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

随机推荐

  1. 交叉熵-loss-理解

    参考链接: https://blog.csdn.net/tsyccnh/article/details/79163834

  2. CodeForces - 627A

    CodeForces - 627Ahttps://vjudge.net/problem/326413/origina+b == (a&b)<<1 +(a^b);然后是位运算,如果对 ...

  3. 技术人自己的KPI

    为什么需要技术KPI 在业务技术团队,有一个不好的趋势,就是团队越来越业务,越来越没有技术味道.每个人都在谈业务,技术大会上在谈业务,周会上在聊业务,周报里写的是业务项目......唯独少被谈及的是技 ...

  4. MongoDB命令的简单操作(一)

    MongoDB是工作在集合和文档上的一种概念. 1.创建数据库 use name2.查看所以的数据库列表 show dbs3.查看当前数据库 db4.向数据库插入数据 db.items.insert( ...

  5. WPF:数据绑定--PropertyChangeNotification属性更改通知

      PropertyChangeNotification属性更改通知 实现效果:1.拍卖金额自动随属性值变化而通知界面绑定的值变化. 关键词 : INotifyPropertyChanged Obse ...

  6. windows--"git安装" 及 "使用git上传项目到github" 详细步骤

    一.下载安装包 https://git-for-windows.github.io/(放在任何一个你想放的地方(系统盘或非系统盘)). 二.开始安装 很简单,双击安装包,一直next下去,到了安装的最 ...

  7. 多机MySQL一主双从详细安装主从复制

    多机MySQL一主双从详细安装 一.复制的工作原理 要想实现AB复制,那么前提是master上必须要开启二进制日志 1.首先master将数据更新记录到二进制日志文件 2.从slave start开始 ...

  8. Oracle JOB的建立,定时执行任务

    Oracle JOB的建立,定时执行任务 oracle job的相关设置 next date: 2010-12-28 18:05:00 interval: to_date(to_char(sysdat ...

  9. day37 07-Hibernate二级缓存:查询缓存

    查询缓存是比二级缓存功能更强大的缓存.必须把二级缓存配置好之后才能用查询缓存,否则是用不了的.二级缓存主要是对类的缓存/对象缓存.查询缓存针对对象也是可以的(因为功能比二级缓存更强大),而且还可以针对 ...

  10. R语言可视化--颜色

    RColorBrewer包 三类调色板:sequential / diverging / qualitative 调色板的信息可以与colorRamp / colorRampPalette结合使用 从 ...