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. MySQL数据库中的索引(二)——索引的使用,最左前缀原则

    上文中,我们了解了MySQL不同引擎下索引的实现原理,在本文我们将继续探讨一下索引的使用以及优化. 创建索引可以大大提高系统的性能. 第一,通过创建唯一性索引,可以保证数据库表中每一行数据的唯一性. ...

  2. List集合、泛型、装箱拆箱

    1.List集合 Vector:增删改查都慢 线程同步 线程安全 LlinkedList:以链表结构存储数据,查询慢.增删快 ArrayList:的运行速度比较快 连续数据空间存储数据,查询快(下标) ...

  3. JavaWeb-SpringSecurity记住我功能

    系列博文 项目已上传至guthub 传送门 JavaWeb-SpringSecurity初认识 传送门 JavaWeb-SpringSecurity在数据库中查询登陆用户 传送门 JavaWeb-Sp ...

  4. 【知识库】-数据库_MySQL 的七种 join

    掘金作者:haifeisi 文章出处: MySQL 的七种 join Learn [已经过测试校验] 一.内连接 二.左外连接 三.右外连接 四.左连接 五.右连接 六.全连接 七.两张表中都没有出现 ...

  5. hadoop-httpfs

    Hadoop-httpfs: client向httpfs提交文件操作,由httpfs和集群交互: 优势:client不必访问集群 WebHDFS API: https://archive.cloude ...

  6. Python最高效爬虫框架

    Overview Scrapy is a fast high-level screen scraping and web crawling framework, used to crawl websi ...

  7. DP&图论 DAY 6 下午 考试

    DP&图论  DAY 6  下午  考试 样例输入 样例输出 题解 >50 pt      dij 跑暴力 (Floyd太慢了QWQ    O(n^3)) 枚举每个点作为起点,dijks ...

  8. 只需体验三分钟,你就会跟我一样,爱上这款Toast

    只需体验三分钟,你就会跟我一样,爱上这款Toast https://www.jianshu.com/p/9b174ee2c571

  9. [SQL Server创建视图时的注意点]

    创建视图的查询语句必须要遵守一定的限制 1. 要对某些列取别名,并保证列名的唯一 (具有相同的列名的表,在创建视图的时候,需要使用别名,表名.列名 也是不可以的) 当我们在通过新建视图来创建视图的话, ...

  10. UI的流畅度优化

    Android中所有的界面绘制工作都是在UI线程中进行的,提高UI流畅度的最核心根本在于释放UI线程.即:不在主线程中做耗时的操作. 很多人都知道,耗时的操作要放到子线程中去做,比如访问网络,比如读写 ...