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 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]:
数组应该考虑到只有一位数的情况
[思维问题]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- "至少2倍"相当于>=
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
- 数组应该考虑到只有一位数的情况
[复杂度]: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比所有数字都大两倍的最大数的更多相关文章
- 【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 ...
- [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 ...
- LeetCode:至少是其他数字两倍的最大数【747】
LeetCode:至少是其他数字两倍的最大数[747] 题目描述 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素 ...
- Java实现 LeetCode 747 至少是其他数字两倍的最大数(暴力)
747. 至少是其他数字两倍的最大数 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 ...
- 【LeetCode】747. Largest Number At Least Twice of Others 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 寻找两次最大值 排序 大顶堆 日期 题目地址:htt ...
- [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 ...
- C#LeetCode刷题之#747-至少是其他数字两倍的最大数( Largest Number At Least Twice of Others)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3746 访问. 在一个给定的数组nums中,总是存在一个最大元素 ...
- [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 ...
- 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 ...
随机推荐
- [EMWIN]关于 GUI_GetPixelIndex 使用的问题
在模拟器上和st单片机上使用以下代码: GUI_COLOR color0,color1; color0 = GUI_GetPixelIndex(rect.x1+1, rect.y0);color1 = ...
- 使用jenkins并发布应用到tomcat
jenkins的介绍及安装请自行百度,本文重点介绍如何使用jenkins,并自动发布web应用到tomcat中. 1 . 创建项目 打开jenkins --> 新建 --> 填写item名 ...
- 剑指offer-第四章解决面试题思路(字符串的排序)
题目:输入一个字符串,打印出该字符串的全排列. 思路:将整个字符串分成两部分,第一部分为一个字符,将该字符和该字符后面的字符(直到最后一个字符)依次交换,确定第一个字符:然后固定第一个字符,将后面的字 ...
- 关于altera的fft核使用问题记录
altera的fft核使用比较特别,今天我做了一下仿真,发现一些问题,现做记录如下: 1,ip配置 parameters选项卡主要是fft变换的长度和数据长度,旋转因子长度,需要注意的是“Twiddl ...
- CSS 属性:touch-action
CSS 属性 touch-action 用于指定某个给定的区域是否允许用户操作,以及如何响应用户操作(比如浏览器自带的划动.缩放等). /* Keyword values */ touch-actio ...
- 百度浏览器极速模式下访问 FastAdmin 的问题
百度浏览器极速模式下访问 FastAdmin 的问题 兼容性问题,因为 https 证书配置时对低版本的浏览器不适配引起. 应该是 百度浏览器的内核太旧,没有更新导致.
- laravel 中config的使用
在laravel的config中添加配置文件(比如:alipay.php)文件内容为return数组的形式 在方法中使用config()函数获取数据 $config = config('alipay. ...
- 使用内部变量,删除,替换,UNSET,等字符操作
使用内部变量,删除,替换,UNSET,等字符操作 FREDDY=freddy 删除字符串前几2个字符: [root@localhost tmp]# echo ${FREDDY:2} eddy ...
- FPGA层次结构和复位策略
FPGA设计中,层次结构设计和复位策略影响着FPGA的时序.在高速设计时,合理的层次结构设计与正确的复位策略可以优化时序,提高运行频率. 设计中,合理的层次结构是我们所追求的. 划分时,按照逻辑分区将 ...
- php单链表实现
php单链表实现 <?php //单链表 class Hero{ public $no; public $name; public $nickname; public $next=null; f ...