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. Codeforces 946 B.Weird Subtraction Process

    B. Weird Subtraction Process   time limit per test 1 second memory limit per test 256 megabytes inpu ...

  2. FMDB使用Cached Statement功能

    FMDB使用Cached Statement功能   在FMDB中,Cached Statement功能是一种提高SQLite数据库访问的技术.在SQLite中,所有的SQL语句都会被编译,形成预处理 ...

  3. Centos7源码安装MySQL5.7

    a.连接数据库报111 从一台linux远程连接另一台linux上的MySQL,出现ERROR 2003 (HY000): Can't connect to MySQL server on 'xxx. ...

  4. Anaconda环境搭建

    最近要使用Anaconda做一些机器视觉相关的开发,在此记录下Anaconda的搭建 首先去官网下载 这里我选择windows平台 由于浏览器自己下载的过慢,我这里选择用迅雷下载 没python就把那 ...

  5. SVG动画实践篇-无中生有的线条动画

    git: https://github.com/rainnaZR/svg-animations/tree/master/src/pages/step2/path 说明 这个动画实现的是线条动画,主要用 ...

  6. Parallel Database for OLTP and OLAP

    Parallel Database for OLTP and OLAP Just asurvey article on materials on parallel database products ...

  7. javascript --- 临时构造器F()

    这篇内容也是之前讲到过的,纯属无聊,莫要见怪.~(- ̄▽ ̄)-~(- ̄▽ ̄)-~(- ̄▽ ̄)-~(- ̄▽ ̄)-~(- ̄▽ ̄)- 正如上文所说,如果所有的prototype属性都指向了一个相同的对象,父 ...

  8. 【bootstrap】Bootstrap Notify的使用步骤

    Bootstrap Notify说明文档:http://bootstrap-notify.remabledesigns.com/ Bootstrap Notify的GitHub地址:https://g ...

  9. Ubuntu下添加开机启动项的2种方法

    1.方法一,编辑rc.loacl脚本 Ubuntu开机之后会执行/etc/rc.local文件中的脚本,所以我们可以直接在/etc/rc.local中添加启动脚本.当然要添加到语句:exit 0 前面 ...

  10. 第七讲_图像描述(图说)Image Captioning

    第七讲_图像描述(图说)Image Captioning 本章结构 递归神经网络 时序后向传播(BPTT) 朴素Vanilla-RNN 基本模型 用sigmoid存在严重的梯度消失 LSTM长短时记忆 ...