[抄题]:

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;
}
}

最新文章

  1. windows7系统下如何安装windows xp系统(无法识别硬盘,删除隐藏分区)
  2. quick cocos 暂停场景
  3. HttpClient_HttpClient 对 cookie的处理
  4. iptables用法
  5. Linux下自带的regex
  6. 利用PC创建一个无线接入点
  7. NGINX和PHP之间的环境变量传递
  8. 委托与Lambda表达式
  9. 求指定范围里的不重复的N个随机数
  10. JS脚本检查密码强度
  11. python安装(python2.7)
  12. 【转载】Python中的正则表达式教程
  13. SQL Server 只安装客户端的方法
  14. ffmpeg-3.2.4-static-win32-for-XP-bin.tar.xz
  15. 如何:配置 ClickOnce 信任提示行为
  16. 爬虫时http错误提示
  17. ubuntu 禁用自带的nouveau显卡驱动,安装NVIDIA显卡驱动
  18. ios开发之 NSObject详解
  19. jquery获取URL的参数和锚点
  20. 黄聪:windowss7显示桌面图标设置在任务栏的解决办法

热门文章

  1. 使用iptables nat进行端口转发
  2. 使用Gson轻松解决复杂结构的Json数据解析
  3. Info.plist字段列表详解
  4. 4 字符串 Swift/Objective -C ——《Swift3.0从入门到出家》
  5. FastAdmin 将 PHP 框架升级到 ThinkPHP 5.1
  6. Java文件压缩优化工具(ProGuard) 软件介绍 Soft content
  7. centos7 &amp; ubuntu14.02安装sublime 3
  8. python3之scrapy安装使用
  9. Git&amp;Repo 命令大全 ***
  10. 蓝桥杯 算法训练 ALGO-21 装箱问题