[抄题]:

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. oracle alter table

    oracle alter table ALTER TABLE (表名) ADD (列名 数据类型); ALTER TABLE (表名) MODIFY (列名 数据类型); ALTER TABLE (表 ...

  2. 剑指offer-第五章优化时间和空间效率(数组中出现次数超过一半的数字)

    题目:输入一个数组,找出一个数字,它在数组中出现的次数超过数组的一半. 题目规定如果可以改变数组中元素的位置. 思路1:如果数组是排序的,那么中间元素的位置不就是次数超过数组一半的元素吗?是的,因此我 ...

  3. django中的@login_required

    转:http://www.cnblogs.com/ccorz/p/Django-zhong-loginrequired-yong-fa-jian-jie.html 1.网站开发时的登录需求: ===用 ...

  4. .NET 应用程序域?

    为了提升windows系统的稳定性与可靠性,windows通过进程来实现.所有的可执行代码.数据以及其他资源都被包含在进程中,不允许其他进程对它进行访问(除非有足够的权限).对于.NET应用程序,还进 ...

  5. RAC修改数据库的spfile位置

    RAC修改spfile位置 [root@rac1 ~]# su - oracle [oracle@rac1 ~]$ sqlplus  / as sysdba SQL*Plus: Release 11. ...

  6. php中 curl模拟post发送json并接收json(转)

    本地模拟请求服务器数据,请求数据格式为json,服务器返回数据也是json. 由于需求特殊性, 如同步客户端的批量数据至云端, 提交至服务器的数据可能是多维数组数据了.  这时需要将此数据以一定的数据 ...

  7. 用dwr封装表单项提交表单

    首先,配置dwr环境,网上很多资料都说得很详细,这里就不写了. dwr封装form表单项,需要用到dwr定义的一个js方法:DWRUtil.getValues(yourform),这个方法可以返回一个 ...

  8. dataView 工具栏

    option = { tooltip : { trigger: 'axis' }, legend: { data:['最高','最低'] }, toolbox: { show : true, orie ...

  9. 题目1534:数组中第K小的数字 ——二分

    http://ac.jobdu.com/problem.php?pid=1534 给定两个整型数组A和B.我们将A和B中的元素两两相加可以得到数组C.譬如A为[1,2],B为[3,4].那么由A和B中 ...

  10. HTML5视频直播及H5直播扫盲

    章来源:http://geek.csdn.net/news/detail/95188 分享内容简介: 目前视频直播,尤其是移动端的视频直播已经火到不行了,基本上各大互联网公司都有了自己的直播产品,所以 ...