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].

思路:

扫描一遍数组,记录最大值与第二大的值,如果最大的是第二大的两倍以上,那么返回索引即可。

 int dominantIndex(vector<int>& nums)
{
int len = nums.size();
int biggest = ,second = ,index = ; for(int i=;i<len;i++)
{
if(nums[i]>biggest)
{
second = biggest;
biggest = nums[i];
index = i;
}
else if(nums[i]>second)
{
second = nums[i];
}
}
if(biggest>=second*)return index;
else return -;
}

[leetcode-748-Largest Number At Least Twice of Others]的更多相关文章

  1. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  2. JavaScript中sort方法的一个坑(leetcode 179. Largest Number)

    在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...

  3. Leetcode:Largest Number详细题解

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

  4. [LeetCode][Python]Largest Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/largest ...

  5. leetcode 179. Largest Number 、剑指offer33 把数组排成最小的数

    这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...

  6. [LeetCode] 179. Largest Number 最大组合数

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

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

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

  8. 【leetcode】Largest Number

    题目简述: Given a list of non negative integers, arrange them such that they form the largest number. Fo ...

  9. leetcode 179. Largest Number 求最大组合数 ---------- java

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

  10. 【leetcode】Largest Number ★

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

随机推荐

  1. 创建<Bean>sessionFactory错误, init方法调用失败;嵌套异常是org.hibernate.exception。

    未知原因:在Maven中hibernate映射开启了自动更新表,出现此异常 org.springframework.beans.factory.BeanCreationException: Error ...

  2. Linux 学习第三天

    一.常用命令 1.diff A.diff -q 源文件 目标文件  (快速比较文件是否相同) 2.ifconfig.nmcli (查看配置信息) 命令输入注意: Windows 查看网卡配置信息输入命 ...

  3. python中的super怎么用?

    面向对象有这个强大特点和作用, 著名的三大特点:封装, 继承, 多态 这篇博客写的是super()的简单理解和使用 今天在读restframework的源码的时候, 发现源码中使用了super, 依以 ...

  4. 集合之WeakHashMap

    WeakHashMap 底层数据结构是哈希表结构 依赖于键的数据结构特点 不同于HashMap,该类的键是以弱键的形式存在 当该键成为垃圾对象,会被垃圾回收期空闲的时候回收,那么改键所对应值也会被回收 ...

  5. Flume采集目录及文件到HDFS案例

    采集目录到HDFS 使用flume采集目录需要启动hdfs集群 vi spool-hdfs.conf # Name the components on this agent a1.sources = ...

  6. python应用:selenium之爬取天眼查信息

    inform_table.py # -*-coding:utf8-*- from selenium import webdriver from selenium.webdriver.common.pr ...

  7. PAT 1001 害死人不偿命的(3n+1)猜想

    1001 害死人不偿命的(3n+1)猜想 (15 分) 卡拉兹(Callatz)猜想: 对任何一个正整数 n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把 (3n+1) 砍掉一半.这样一直反复 ...

  8. DNS服务器的简介——2

    生成HTTP报文后,因为我们输入的是服务器的域名,但在包体的发送中需要的是服务器的IP地址,所以接下来的工作就是查找服务器的IP地址,而查询IP地址,就需要DNS服务器来帮忙了. IP地址简介: IP ...

  9. C语言实验报告(五) 两个正整数的最大公约数

    编程实现求两个正整数的最大公约数,要求计算最大公约数用函数fun(int a,int b)实现. #include<stdio.h>void main(){  int n,a,b;  in ...

  10. 解决应用程序无法正常启动0xc0150002等问题

    1.在程序运行出错的时候,右键“我的电脑”,然后点击“管理”→“事件查看器”→“Windows 日志”→“应用程序”,查看错误信息: 1> “E:\IPCam_share\ARP\數據處理\Hg ...