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.

记录,第一大的数和第二大的数,判断2倍关系就行了。

class Solution {
public:
int dominantIndex(vector<int>& nums) {
int max1 = -1;
int max2 = -1;
int id = 0;
for(int i = 0; i < nums.size(); ++i) {
int x = nums[i];
if (x > max1) max2 = max1, max1 = x, id = i;
else if (x > max2) max2 = x;
}
if (max1 >= 2*max2) return id;
return -1;
}
};

leetcode 747. Largest Number At Least Twice of Others的更多相关文章

  1. [LeetCode] 747. Largest Number At Least Twice of Others_Easy

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

  2. 【Leetcode_easy】747. Largest Number At Least Twice of Others

    problem 747. Largest Number At Least Twice of Others 题意: solution1: class Solution { public: int dom ...

  3. 【LeetCode】Largest Number 解题报告

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

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

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

  5. Leetcode:Largest Number详细题解

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

  6. [LeetCode][Python]Largest Number

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

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

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

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

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

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

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

随机推荐

  1. HTTP状态码之200和304

    HTTP状态码之200和304   HTTP状态码(HTTP Status Code)是一种表示网页服务器响应状态的三位数字编码.通过这些数字,可以简化状态的表达.状态码有几十种,其中首位数字为1-5 ...

  2. spring事物,在service层如果进行了异常处理,则不会回滚

    今天进行了事物处理的验证,发现如果在在service层如果进行了异常处理,则不会回滚. 看来异常的处理还是统一放在controller层比较好,service如果是查询方法,出现了异常,就不要做处理了 ...

  3. BZOJ1014火星人prefix Splay維護序列 + 字符串哈希

    @[Splay, 哈希] Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:\(madamimadam\), 我们将这个字符串的各个字符予以标号 ...

  4. nginx学习(二)——基础概念之异步非阻塞

    上面讲了很多关于nginx的进程模型,接下来,我们来看看nginx是如何处理事件的. 有人可能要问了,nginx采用多worker的方式来处理请求,每个worker里面只有一个主线程,那能够处理的并发 ...

  5. Highcharts常用的最核心的参数选项配置详细说明

    Highcharts提供大量的选项配置参数,您可以轻松定制符合用户要求的图表,目前官网只提供英文版的开发配置说明文档,而中文版的文档网上甚少,且零散不全.这里,我把Highcharts常用的最核心的参 ...

  6. matplotlib简易新手教程及动画

    做数据分析,首先是要熟悉和理解数据.所以掌握一个趁手的可视化工具是很重要的,否则对数据连个主要的感性认识都没有,怎样进行下一步的design 点击打开链接 还有一个非常棒的资料  Matplotlib ...

  7. python(5)- 简单练习:python三级菜单优化

    python三级菜单优化,菜鸟版链接:http://www.cnblogs.com/xuyaping/p/6648170.html menu = { '北京':{ '海淀':{ '五道口':{ 'so ...

  8. web前端面试系列 - 算法( 数组去重 )

    1. 思路:设置一个临时数组temp,然后遍历要去重的数组arr,如果arr中的元素能够在temp中找到,则跳过此元素,否则将此元素存入temp,最后返回temp. 实现一 function uniq ...

  9. Kuebernetes之DaemonSet

    系列目录 DaemonSet确保集群中每个(部分)node运行一份pod副本,当node加入集群时创建pod,当node离开集群时回收pod.如果删除DaemonSet,其创建的所有pod也被删除,D ...

  10. kubernetes集群管理命令(三)

    系列目录 前面两节我们由浅入深介绍了不少kubernetes管理比较常用的命令.本节我们通过案例讲解一些需要更为复杂的操作才能完成的命令. 选择一个deployment下的所有pod 前面讲到过,ku ...