题目描述:

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:

  1. nums will have a length in the range [1, 50].
  2. Every nums[i] will be an integer in the range [0, 99].

要完成的函数:

int dominantIndex(vector<int>& nums)

说明:

给定一个vector,要求判断这个vector中的最大值是不是至少等于其他所有元素值的两倍,如果是的话,返回最大值的位置,如果不是,返回-1。

这道题其实相当容易,就是求vector的最大值和次大值,以及在求最大值的过程中记录一下最大值的位置。

我们考虑一下边界条件,只有一个元素和只有两个元素的情况,接着构造一般情况下的代码,如下:

   int dominantIndex(vector<int>& nums)
{
int s1=nums.size();
if(s1==1)//只有一个元素的边界条件
return 0;
else if(s1==2)//只有两个元素的边界条件
{
if(nums[0]>nums[1])
{
if(nums[0]>=2*nums[1])
return 0;
else
return -1;
}
else
{
if(nums[1]>=2*nums[0])
return 1;
else
return -1;
}
}
int max1,max2,i=2,index;
if(nums[0]>=nums[1])
{
max1=nums[0];
max2=nums[1];
index=0;
}
else
{
max1=nums[1];
max2=nums[0];
index=1;
}
while(i<s1)//i从2开始
{
if(nums[i]>=max1)
{
max2=max1;
max1=nums[i];
index=i;
}
else
{
if(nums[i]>=max2)
max2=nums[i];
}
i++;
}
if(max1>=2*max2)
return index;
else
return -1;
}

上述代码虽然条件判断语句多了点,但大体上来看没有浪费很多时间,代码也不难理解。

实测9ms,beats 81.63% of cpp submissions。

leetcode-747-Largest Number At Least Twice of Others(求vector的最大值和次大值)的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 【LeetCode】747. Largest Number At Least Twice of Others 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 寻找两次最大值 排序 大顶堆 日期 题目地址:htt ...

  4. 【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 ...

  5. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  6. JavaScript中sort方法的一个坑(leetcode 179. Largest Number)

    在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...

  7. Leetcode:Largest Number详细题解

    题目 Given a list of non negative integers, arrange them such that they form the largest number. For e ...

  8. [LeetCode][Python]Largest Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/largest ...

  9. leetcode 179. Largest Number 、剑指offer33 把数组排成最小的数

    这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...

随机推荐

  1. 477. Total Hamming Distance总的二进制距离

    [抄题]: The Hamming distance between two integers is the number of positions at which the correspondin ...

  2. 657. Judge Route Circle机器人能否返回

    [抄题]: Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this r ...

  3. Mac os安装DVWA环境教程

    Mac os安装DVWA环境教程 1.尽管Mac自带Apache和php 事实上Mac和WIN都用XAMPP一键化安装比较方便 2.解压DVWA-master 改名为dvwa移动到XAMPP的目录 3 ...

  4. 3.1.2 condition 条件

    package 第三章.重入锁; import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Reent ...

  5. [GO]获取命令行参数

    package main import ( "os" "fmt" ) func main() { list := os.Args n := len(list) ...

  6. Open Type vs Open resource

    Open Type 可查询所有java 类型,包括.java .class Open Resource 只能打开 .java 等 ,不能打开 .class 相同点 都可以使用 ? * 通配符

  7. getTextContent()方法会出现The method getTextContent() is undefined for the type Node 提示

    eclipse 中 如果加入了 其他了xfire 等其他xml解析包的话,使用org.w3c.dom.Node下的getTextContent()方法会出现The method getTextCont ...

  8. linux 磁盘 分区 MBR GPT

    磁盘:由许多盘片,机械手臂,磁头和主轴马达所构成的,实际的数据都是写入盘片上面,而读写主要是通过机械手臂可伸展让磁头在盘片(在主轴马达的转动左右下,盘片跟着转动)上面进行读写操作.由于单一盘片写入数据 ...

  9. linux 常见命令--系统信息部分

    head -n 1 /etc/issue    #查看操作系统 cat /etc/redhat-release  #查看linux系统版本 cat /proc/meminfo |grep MemTot ...

  10. c#进阶之浅析委托和事件

    何为委托 加了delegate关键字,没有方法体{}的方法模版(方法列表);委托是一种类型 public void Write() { //TODO } //加上关键字delegate,去掉方法体{} ...