[抄题]:

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. 关于niosii不同版本的ip核不兼容的问题

    这次用到网上下载的一个12.0版本的ip核,使用qsys做的,而我的开发环境是10.1的,sopc是用的sopcbuilder做的,下载下来的ip核添加新组建后,会报错,采取的做法是对比我的10.1版 ...

  2. zju 校队选拔 被虐记

    选拔已经开始了三天才想起来写游记 QAQ.. 7.12 弱弱的Sky_miner来到了ZJU,过程中被热成狗... 然后见到了无数大二大三的大佬们,过程中被热成狗... 后来听靖哥哥说集训的注意事项, ...

  3. Spring aop 记录操作日志 Aspect 自定义注解

    时间过的真快,转眼就一年了,没想到随手写的笔记会被这么多人浏览,不想误人子弟,于是整理了一个优化版,在这里感谢智斌哥提供的建议和帮助,话不多说,进入正题 所需jar包 :spring4.3相关联以及a ...

  4. Ubantu中安装sublime

    Sublime-text-3的安装步骤   添加Sublime-text-3软件包的软件源 sudo add-apt-repository ppa:webupd8team/sublime-text-3 ...

  5. android Shape使用(转)

    在Android中常常会使用shape来定义控件的一些显示属性,那么怎么来用那,今天我们就来看一些shape的使用,大家看完这篇以后就会对shape有了大体的了解,下面的是稍作总结请大家仔细的观看: ...

  6. erlang热部署

    以下流程参考rebar的wiki,亲测 rebar的版本一定要注意,高版本对于下面两个指令有bug rebar generate-appups rebar generate-upgrade 经过一个个 ...

  7. 你知道PORT吗?

    在TCP协议中,有端口(PORT)的概念,很多人都不知道端口到底是什么.之前介绍过物理地址,也就是网卡地址,做个不恰当的比喻,物理地址(MAC)地址,相当于身份证(唯一),家庭地址是几幢几单元相当于I ...

  8. Linux安装JRE tomcat配置java环境

    安装JRE 到http://www.oracle.com/technetwork/java/javase/downloads/index.html下载JRE软件. 1.wget http://down ...

  9. Sql Server优化之路

    本文只限coder级别层次上对Sql Server的优化处理简结,为防止专业DB人士有恶心.反胃等现象,请提前关闭此页面. 首先得有一个测试库,使用数据生成计划生成测试数据库(参考:http://de ...

  10. python学习笔记(四):函数

    一.函数是什么? 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,编程中的函数在英文中也有很多不同的叫法.在BASIC中叫做subroutine(子过程或子程序),在Pasc ...