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. 分析:这题就是让你找出第一大和第二大的数。
 class Solution {
public int dominantIndex(int[] nums) {
int max = -, index = -, second = -;
for (int i = ; i < nums.length; i++) {
if (nums[i] > max) {
second = max;
max = nums[i];
index = i;
} else if (nums[i] > second)
second = nums[i];
}
return second * <= max ? index : -;
}
}

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

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

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

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

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

  3. 【leetcode】Largest Number

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

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

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

  5. LeetCode-179. Largest Number

    179. Largest Number Given a list of non negative integers, arrange them such that they form the larg ...

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

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

  7. 【leetcode】Largest Number ★

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

  8. Largest Number

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

  9. Java for LeetCode 179 Largest Number

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

  10. 179. Largest Number -- 数字字符串比较大小

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

随机推荐

  1. Laravel 引入第三方类库及自定义函数

    1.新建一个目录放第三方类库 2.找到composer.json文件打开,在里面autoload 下classmap下面加入类库路径 3根目录下运行composer dumpautoload 4.使用 ...

  2. 《剑指offer》算法题第五天

    今日题目: 反转链表 合并两个排序的链表 树的子结构 二叉树的镜像 对称二叉树 今日重点是1反转链表,3树的子结构,以及5对称二叉树. 1. 反转链表 题目描述: 输入一个链表,反转链表后,输出链表的 ...

  3. UVA 315 求割点 模板 Tarjan

    D - D Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Pract ...

  4. TTTTTTTTTTTTTTTTTT CodeForces 589A Email Aliases 字符串 map

    A - Email Aliases Time Limit:2000MS     Memory Limit:524288KB     64bit IO Format:%I64d & %I64u ...

  5. puppet自动化部署

    puppet自动化部署 puppet  实现运维自动化管理的软件. 官方网站: http://puppetlabs.com/    pupptet下载链接:http://downloads.puppe ...

  6. JavaScript数字计算精度丢失的问题和解决方案

    一.JS数字精度丢失的一些典型问题 1. 两个简单的浮点数相加:0.1 + 0.2 != 0.3 // true,下图是firebug的控制台截图: 看看java的计算结果:是不是让你很不能接受 再来 ...

  7. Java多线程核心知识(跳槽面试必备)

    多线程相对于其他 Java 知识点来讲,有一定的学习门槛,并且了解起来比较费劲.在平时工作中如若使用不当会出现数据错乱.执行效率低(还不如单线程去运行)或者死锁程序挂掉等等问题,所以掌握了解多线程至关 ...

  8. 阿里云OSS细粒度权限控制

    做下工作记录: 自定义安全策略,然后授权即可 { ", "Statement": [ { "Effect": "Allow", & ...

  9. TCP连接建立 之 同时打开

    假设两台设备双方均发送syn给对端,在发送syn之后状态处于SYN_SENT状态,此时双方均收到对端的发来的syn,则立即进入SYN_RECV状态,并且都向对端回复syn+ack,在收到syn+ack ...

  10. 在.slurm文件中激活Anaconda环境

    超算中心使用slurm作为集群调度.原始slurm脚本如下: source activate tensorflow-gpu python neural_style.py --content conte ...