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 dingo,HTTP的请求头(accept)无法携带版本号的解决方法

    Laravel dingo,HTTP的请求头(accept)无法携带版本号的解决方法  2017年9月6日  原创分享  zencodex  使用 Laravel dingo 做api开发时,涉及 A ...

  2. .net文件夹上传源码

    核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...

  3. 灰度图像--图像分割 阈值处理之OTSU阈值

    学习DIP第55天 转载请标明本文出处:***http://blog.csdn.net/tonyshengtan ***,出于尊重文章作者的劳动,转载请标明出处!文章代码已托管,欢迎共同开发:http ...

  4. codeforces269B

    Greenhouse Effect CodeForces - 269B Emuskald is an avid horticulturist and owns the world's longest ...

  5. Gitee使用方法

    原文链接:https://www.cnblogs.com/yiven/p/8465054.html 1.创建仓库: a.创建远程仓库 登入Gitee后,点击头像旁边的"+"加号-- ...

  6. git 出现错误 Could not resolve host: github.com 或者 gitlab.com 或者gerrit相关( 自有服务 )

    原来是因为github.com没有被主机给解析 1.第一步是 ping 你的gitlab 或者 github服务器ip地址 如果每隔几秒有 time = xx.ms 刷新 就证明是通的 2. 编辑 e ...

  7. LeetCode 147. 对链表进行插入排序(Insertion Sort List)

    题目描述 对链表进行插入排序. 插入排序的动画演示如上.从第一个元素开始,该链表可以被认为已经部分排序(用黑色表示). 每次迭代时,从输入数据中移除一个元素(用红色表示),并原地将其插入到已排好序的链 ...

  8. js小数点相乘或相除出现多位数的问题

    最近做一个支付的项目需要做个计算器,所以发现了一个问题. 比如: 0.03/0.00003=999.9999999999999 0.0003*0.3=0.000029999999999999997 0 ...

  9. js 处理url参数,应用导航分类

    1.先上图 2.代码 html <li><a href="javascript:void(0);" data-cid = "{$v['id']}&quo ...

  10. 4.HadoopMapRe程序设计

    1.介绍 2.基本构架与组件 3.基本算法