[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 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.
Note:
numswill have a length in the range[1, 50].- Every
nums[i]will be an integer in the range[0, 99].
这道题让我们找一个至少是其他数字两倍的最大数字,那么我们想,首先明确的一点是这个要求的数字一定是数组中的最大数字,因为其是其他所有的数字的至少两倍。然后就是,如果该数字是数组中第二大的数字至少两倍的话,那么它一定是其他所有数字的至少两倍,所以我们可以遍历一次数组分别求出最大数字和第二大数字,然后判断一下最大数字是否是第二大数字的两倍即可,注意这里我们判断两倍的方法并不是直接相除,为了避免除以零的情况,我们采用减法,参见代码如下:
解法一:
class Solution {
public:
int dominantIndex(vector<int>& nums) {
int mx = INT_MIN, secondMx = INT_MIN, mxId = ;
for (int i = ; i < nums.size(); ++i) {
if (nums[i] > mx) {
secondMx = mx;
mx = nums[i];
mxId = i;
} else if (nums[i] > secondMx) {
secondMx = nums[i];
}
}
return (mx - secondMx >= secondMx) ? mxId : -;
}
};
当然我们也可以使用更straightforward的方法,首先遍历一遍数组找出最大数字,然后再遍历一遍数组,验证这个数字是否是其他数字的至少两倍,参见代码如下:
解法二:
class Solution {
public:
int dominantIndex(vector<int>& nums) {
int mx = INT_MIN, mxId = ;
for (int i = ; i < nums.size(); ++i) {
if (mx < nums[i]) {
mx = nums[i];
mxId = i;
}
}
for (int num : nums) {
if (mx != num && mx - num < num) return -;
}
return mxId;
}
};
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Largest Number At Least Twice of Others 至少是其他数字两倍的最大数的更多相关文章
- Leetcode747.Largest Number At Least Twice of Others至少是其他数字两倍的最大数
在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 1: 输入: nums = [3, ...
- LeetCode:至少是其他数字两倍的最大数【747】
LeetCode:至少是其他数字两倍的最大数[747] 题目描述 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素 ...
- [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 ...
- [LC]747题 Largest Number At Least Twice of Others (至少是其他数字两倍的最大数)
①中文题目 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 1: 输入: nums ...
- Java实现 LeetCode 747 至少是其他数字两倍的最大数(暴力)
747. 至少是其他数字两倍的最大数 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 ...
- C#LeetCode刷题之#747-至少是其他数字两倍的最大数( Largest Number At Least Twice of Others)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3746 访问. 在一个给定的数组nums中,总是存在一个最大元素 ...
- [LeetCode] Largest Number 最大组合数
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Leetcode Largest Number c++ solution
Total Accepted: 16020 Total Submissions: 103330 Given a list of non negative integers, arrange t ...
- LeetCode: Largest Number 解题报告 以及Comparator, CompareTo 应用
Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...
随机推荐
- Java虚拟机之Java内存区域
Java虚拟机运行时数据区域 ⑴背景:对于c/c++来说程序员来说,需要经常去关心内存运行情况,但对于Java程序员,只需要在必要时关心内存运行情况,这是因为在Java虚拟机自动内存管理机制的帮助下, ...
- 微信APP简要分析
Part1 走进微信APP 很明显,微信是很成功的APP. 微信 (WeChat) 是腾讯公司于2011年1月21日推出的一个为智能终端提供即时通讯服务的免费应用程序,现已是超过九亿人使用的手机应用. ...
- 冲刺No.3
Alpha冲刺第三天 站立式会议 项目进展 今日团队对CSS与JS的基础知识进行了应用,并对网站的UI设计进行了讨论,对数据库设计进行了进一步的探讨,基本确立了各个表单的结构和内容.分割出项目基本模块 ...
- iOS开发所有KeyboardType与图片对应展示
1.UIKeyboardTypeAlphabet 2.UIKeyboardTypeASCIICapable 3.UIKeyboardTypeDecimalPad 4.UIKeyboardTypeDe ...
- v7000数据恢复_MDisk重建数据恢复方法(北亚数据恢复)
很多工程师都有这样的疑问,MDisk重建后还能不能恢复数据呢?应该怎么做才能恢复数据呢?本文这里就以IBM V7000存储为例,详细讲解因为某个MDisk被重建导致的数据丢失的恢复方法.我们本案例中的 ...
- CSS你所不知的伪元素的用法
你所不知的 CSS ::before 和 ::after 伪元素用法 博客分类: Div / Css / XML / HTML5 CSS 有两个说不上常用的伪类 :before 和 :after, ...
- PHP截取日期
date( 'Y-m-d ',strtotime('2017-10-9 12:23:35')) 通过时间格式,获取的是2017-10-9
- 关于kali linux 2.0的vmware tools的安装问题
在安装好kali linux 2.0 后,首先要做的就是添加源并更新系统,否则会出现软件定位问题. 在kali 2.0中,vmware tools已经不能使用了,官方放了一个工具下载安装就好. 添加源 ...
- threadlocal原理及常用应用场景
1.深入解析ThreadLocal类 ThreadLocal类提供的几个方法: public T get() { } public void set(T value) { } public void ...
- SpringCloud的Bus(一)消息中间件的概念和用途
一.概念与定义 1.Message Broker Message Broker是一种消息验证.消息转换.消息路由的架构模式,用于如: 消息路由到一个或多个目的地 消息转化为其他的表现方式 执行消息的聚 ...