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 ...
随机推荐
- 一个Filter需要配置多个url-pattern
最近做项目遇到一个Filter需要配置多个url-pattern,上网查了下资料,经测试,现总结下 一.完全错误的方式 Java代码 <filter> <filter-name> ...
- linux系统文件的链接
一. 硬链接(实际链接) (以linux系统为例) 1. 文件的索引节点inode 假设我们在硬盘当前目录下建立了一个名为mytext文本文件,其内容只有一行:This is my file. (1) ...
- 15-糗事百科(python+xpath)
爬取糗事百科的段子: 1.用xpath分析首要爬去内容的表达式: 2.用发起请求,获得原码: 3.用xpath分析源码,提取有用信息: 4.由python格式转为json格式,写入文件 #_*_ co ...
- servlet中如何发送ajax请求并动态拼接数据到html中
废话不多说,直接上代码 1.servlet 2.js 3.jsp 有不懂得欢迎来扣我哦^_^ 详细介绍请查看全文:https://cnblogs.com/qianzf/ 原文博客的链接地址:https ...
- PHP Windows系统下调用OpenOffice
项目需要把用户上传的word文档转换为pdf文件,方便用户浏览.经过谷歌百度找到PHP可以使用COM组件调用微软的openoffice来实现文档转换 1,安装OpenOffice 安装OpenOffi ...
- 浅谈Oracle12c 数据库、用户、CDB与PDB之间的关系
名词介绍: 数据库:数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生 于距今六十多年前,随着信息技术和市场的发展,特别是二十世纪九十年代以 后,数据管理不再仅仅是存储和管理 ...
- 编写高质量代码改善C#程序的157个建议——建议156:利用特性为应用程序提供多个版本
建议156:利用特性为应用程序提供多个版本 基于如下理由,需要为应用程序提供多个版本: 应用程序有体验版和完整功能版. 应用程序在迭代过程中需要屏蔽一些不成熟的功能. 假设我们的应用程序共有两类功能: ...
- Android PendingIntent小结
官方文档的解释 A description of an Intent and target action to perform with it. 注意有一个target action,既Pen ...
- [LeetCode 题解]: Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- 记一次艰苦卓绝的Discuz x3 论坛升级过程
首先吐槽一下discuz 的官方论坛. 你要想下载到正确版本的discuz实在不容易找到. 有兴趣自己去看吧. 就是因为这个原因, 我本来想要安装x2.5版本(那时x3 还是Beta版本), 结果不小 ...