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:
nums
will 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 ...
随机推荐
- 小程序开发运营必看:微信小程序平台运营规范
一.原则及相关说明 微信最核心的价值,就是连接——提供一对一.一对多和多对多的连接方式,从而实现人与人.人与智能终端.人与社交化娱乐.人与硬件设备的连接,同时连接服务.资讯.商业. 微信团队一 ...
- logback与log4j比较
摘自:https://www.cnblogs.com/smile361/p/7592684.html logback与log4j比较 更快的执行速度: 基于我们先前在log4j上的工作,logback ...
- Java设计模式(3)——抽象工厂模式
抽象工厂模式是所有形态的工厂模式中最为抽象和最其一般性的.抽象工厂模式可以向客户端提供一个接口,使得客户端在不必指定产品的具体类型的情况下,能够创建多个产品族的产品对象. 一.产品族和产品等级结构 为 ...
- 设计模式(java)--状态模式
状态模式(State Pattern)是设计模式的一种,属于行为模式. 定义(源于Design Pattern):当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类. 状态模式主要 ...
- java网站如何集成facebook第三方登录
第一次接触Facebook第三方登录,可能有些地方做的并不全面,只是尝试着做了一个小demo,因为国内接入Facebook的项目并不多,并且多数都是Android或iOS的实现,所以资料也特别少,在此 ...
- Dubbo的配置及启动
Tomcat+Dubbo安装 1.将tomcat的webapps目录下的所有文件清空,讲Dubbo管理控制台的程序dubbo-admin-2.5.3.war放 到webapps中,并且解压命名为ROO ...
- (转)DataTable与结构不同实体类之间的转换
原文地址:http://www.cnblogs.com/kinger906/p/3428855.html 在实际开发过程中,或者是第三方公司提供的数据表结构,与我们系统中的实体类字段不对应,遇到这样我 ...
- 编写高质量代码改善C#程序的157个建议——建议142:总是提供有意义的命名
建议142:总是提供有意义的命名 除非有特殊原型,否则永远不要为自己的代码提供无意义的命名. 害怕需要过长的命名才能提供足够的意义?不要怕,其实我们更介意的是在代码的时候出现一个iTemp. int ...
- xml构建
<a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=346252320&site=qq ...
- [LeetCode题解]: Sort Colors
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given an a ...