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 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.
记录,第一大的数和第二大的数,判断2倍关系就行了。
class Solution {
public:
int dominantIndex(vector<int>& nums) {
int max1 = -1;
int max2 = -1;
int id = 0;
for(int i = 0; i < nums.size(); ++i) {
int x = nums[i];
if (x > max1) max2 = max1, max1 = x, id = i;
else if (x > max2) max2 = x;
}
if (max1 >= 2*max2) return id;
return -1;
}
};
leetcode 747. Largest Number At Least Twice of Others的更多相关文章
- [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_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 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- JavaScript中sort方法的一个坑(leetcode 179. Largest Number)
在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...
- Leetcode:Largest Number详细题解
题目 Given a list of non negative integers, arrange them such that they form the largest number. For e ...
- [LeetCode][Python]Largest Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/largest ...
- leetcode 179. Largest Number 、剑指offer33 把数组排成最小的数
这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...
- [LeetCode] 179. Largest Number 最大组合数
Given a list of non negative integers, arrange them such that they form the largest number. Example ...
- Java 特定规则排序-LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
随机推荐
- 洛谷—— P1134 阶乘问题
https://www.luogu.org/problemnew/show/P1134 题目描述 也许你早就知道阶乘的含义,N阶乘是由1到N相乘而产生,如: 12! = 1 x 2 x 3 x 4 x ...
- Nessus虚拟机的几个问题解决办法
1.使用ppp的校园网或者家庭宽带无法通过桥接上网. 这时要把这俩网卡变成NAT模式就行. 2.国外下载插件包(或者过慢). 我这里贡献个高速链接.base64,懂得自然懂. c3NyOi8vTkRj ...
- Linux 在VMware中搭建CentOS6.5虚拟机
原文:http://www.cnblogs.com/PurpleDream/p/4263465.html Linux 在VMware中搭建CentOS6.5虚拟机 前言: 本文主要是我在大家 ...
- 强大的开源网络侦查工具:IVRE
IVRE简介 IVRE(又名DRUNK)是一款开源的网络侦查框架工具,IVRE使用Nmap.Zmap进行主动网络探测.使用Bro.P0f等进行网络流量被动分析,探测结果存入数据库中,方便数据的查询.分 ...
- 将可执行程序的内存空间扩展到3GB(windows)
为了告知操作系统这个应用程序可以支持/3GB方式,我们需要往exe 文件头中添加一个 IMAGE_FILE_LARGE_ADDRESS_AWARE 标志.添加的方式很简单: 在你的系统的 Progra ...
- com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'user'
1.错误描写叙述 2014-7-12 21:06:05 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager 信息: ...
- mac os PHP 访问MSSQL
写在前: 项目的数据库是sql server,但是自己的系统是mac os.这样导致了需要一个烦人的系统环境搭建过程.目前要在mac 上的php环境中支持mssql环境访问,经过自己了解,有两种方式: ...
- c# vitural
virtual关键字用于指定属性或方法在派生类中重写. 默认情况下,派生类类从其基类继承属性和方法,如果继承的属性或方法需要在派生类中有不同的行为,则可以重写它,即可以在派生类中定义该属性或方法的新实 ...
- Django开发微信公众平台
处理微信发来的信息,实际上就是处理xml的过程.先写xml工具类 # -*- coding:utf-8 -*- from xml.dom import minidom from Web.model.W ...
- 《UNIX-Shell编程24学时教程》读书笔记Chap1,2 Shell基础,脚本基础
Chap1 Shell基础 知道该使用哪种命令是依赖于经验的.----惟手熟尔. 1.1 什么是命令 其实知道这些名词好像也没什么帮助,嘻嘻 1.2 什么是Shell 不同用户不同的提示符:不同的环境 ...