leetcode-747-Largest Number At Least Twice of Others(求vector的最大值和次大值)
题目描述:
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].
要完成的函数:
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的最大值和次大值)的更多相关文章
- [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 ...
- 【LeetCode】747. Largest Number At Least Twice of Others 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 寻找两次最大值 排序 大顶堆 日期 题目地址:htt ...
- 【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 ...
随机推荐
- MAXOS 进程管理
ps -ef|grep +程序名 注意进程名区分大小写 linux上进程有5种状态:1. 运行(正在运行或在运行队列中等待)2. 中断(休眠中, 受阻, 在等待某个条件的形成或接受到信号)3. 不可中 ...
- Python爬虫实战八之利用Selenium抓取淘宝匿名旺旺
更新 其实本文的初衷是为了获取淘宝的非匿名旺旺,在淘宝详情页的最下方有相关评论,含有非匿名旺旺号,快一年了淘宝都没有修复这个. 可就在今天,淘宝把所有的账号设置成了匿名显示,SO,获取非匿名旺旺号已经 ...
- PBOC中文件结构,文件类型解析
1.明确两个规范,a. ISO7816 b.EMV规范/PBOC规范,二者的区别,7816是ISO制定的,是国际规范,而EMV规范是卡组织制定的,是遵循ISO7816规范的,PBOC是抄袭EMV规 ...
- Spring 注解 整理
首先 在xml中配置 xmlns:context="http://www.springframework.org/schema/context" http://www.spring ...
- Python - excel 详解
安装 pip install xlrd # 读xlspip install xlwt # 写xlspip install xlutils # 改写xls 读取 Excel ...
- spring mvc 集成hibernate步骤
今天从头把hibernate集成进入springMVC框架中,把过程记录下来. 1.首先要在监听器配置文件中加入hibernate支持,如下: <?xml version="1.0&q ...
- 使用GeoServer+QGIS发布WMTS服务 | Publishing WMTS Service Using GeoServer+QGIS
Web GIS系列: 1.搭建简易Web GIS网站:使用GeoServer+PostgreSQL+PostGIS+OpenLayers3 2.使用GeoServer+QGIS发布WMTS服务 3.使 ...
- Maven项目常见错误
一.Cannot change version of project facet Dynamic Web Module to 3.0. 和 One or more constraints have n ...
- 自己动手设计并实现一个linux嵌入式UI框架(设计)
看了"自己动手设计并实现一个linux嵌入式UI框架"显然没有尽兴,因为还没有看到庐山真面目,那我今天继续,先来说说,我用到了哪些知识背景.如:C语言基础知识,尤其是指针.函数指针 ...
- vs附加调试
已解决.项目-属性-调试-启用调试器选择启用本机代码调试,不知道怎么的选成启用Visual Studio承载进程了.