[抄题]:

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.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

数组应该考虑到只有一位数的情况

[思维问题]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. "至少2倍"相当于>=

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

  1. 数组应该考虑到只有一位数的情况

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

414三位数

[代码风格] :

class Solution {
public int dominantIndex(int[] nums) {
//cc
if (nums == null || nums.length == 0) {
return -1;
}
if (nums.length == 1) {
return 0;
} //ini
int max2 = Integer.MIN_VALUE, max1 = Integer.MIN_VALUE + 1, index = 0; //for loop, change max1 max2
for (int i = 0; i < nums.length; i++) {
if (nums[i] > max1) {
max2 = max1;
max1 = nums[i];
index = i;
}else if (nums[i] > max2) {
max2 = nums[i];
}
} //return
return (max1 >= 2 * max2) ? index : -1;
}
}

747. Largest Number At Least Twice of Others比所有数字都大两倍的最大数的更多相关文章

  1. 【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 ...

  2. [LeetCode] Largest Number At Least Twice of Others 至少是其他数字两倍的最大数

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

  3. LeetCode:至少是其他数字两倍的最大数【747】

    LeetCode:至少是其他数字两倍的最大数[747] 题目描述 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素 ...

  4. Java实现 LeetCode 747 至少是其他数字两倍的最大数(暴力)

    747. 至少是其他数字两倍的最大数 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 ...

  5. 【LeetCode】747. Largest Number At Least Twice of Others 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 寻找两次最大值 排序 大顶堆 日期 题目地址:htt ...

  6. [Swift]LeetCode747. 至少是其他数字两倍的最大数 | Largest Number At Least Twice of Others

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

  7. C#LeetCode刷题之#747-至少是其他数字两倍的最大数( Largest Number At Least Twice of Others)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3746 访问. 在一个给定的数组nums中,总是存在一个最大元素 ...

  8. [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 ...

  9. leetcode 747. Largest Number At Least Twice of Others

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

随机推荐

  1. (六)js常见四大排序

    今天突然想回顾一下四大排序,虽然说在实战中没有用到,但是想回顾一下四大排序的思想   var arr = [23, 34, 11, 22, 19, 18];   1.冒泡排序: 冒泡排序的思路分析: ...

  2. proxifier 注册码 +电脑全局代理设置

    L6Z8A-XY2J4-BTZ3P-ZZ7DF-A2Q9C(Portable Edition)  5EZ8G-C3WL5-B56YG-SCXM9-6QZAP(Standard Edition)  P4 ...

  3. spring源码学习之:项目公共配置项解决方案

    一:项目中有一些key,value的简单配置 org.apache.commons.configuration.DatabaseConfiguration可以轻松解决 二:配置项目的xml中bean ...

  4. Oracle变量的定义、赋值及使用

    首先我们来看看代码,然后我们在说明和解释代码: declare l_dept ; currtime date := sysdate; l_nam ) :),'yyyymmdd'); -- to_cha ...

  5. 洛谷 1291 [SHOI2002]百事世界杯之旅

    题目:https://www.luogu.org/problemnew/show/P1291 大水题!套路!模板! 稍微注意一下输出就行了. #include<iostream> #inc ...

  6. DLatch by Verilog

    //-----------------------------------------------------// Design Name : dlatch_reset// File Name   : ...

  7. WCF 身份验证 通过检查客户端IP

    WCF 身份验证 功能描述: 服务运行的时候,通过配置文件获取所有可访问SOA端的服务IP.每次客户调用服务时获取IP对比判定通过. 以下是获取客户端IP的代码: /***************** ...

  8. js大法处理富文本输入

  9. chrome浏览器,调试详解,调试js、调试php、调试ajax

    1.可以看到js报错 2.可以看到php报错 3.可以看到ajax返回的信息,以及报错情况 4.可以看到调用了那些内容以及顺序 5.可以调试js代码 6.可以查看session等等 功能非常之强大. ...

  10. python开发进程:进程开启方式&多进程

    一,进程的开启方式 利用模块开启进程 from multiprocessing import Process import time,random import os def piao(name): ...