【Leetcode_easy】747. Largest Number At Least Twice of Others
problem
747. Largest Number At Least Twice of Others
题意:
solution1:
class Solution {
public:
int dominantIndex(vector<int>& nums) {
int mx = -, secondMx = -, mxId = -;//err...
if(nums.size()<) return -;
for(int i=; i<nums.size(); i++)
{
if(nums[i]>mx)
{
secondMx = mx;
mx = nums[i];
mxId = i;
}
else if(nums[i]>secondMx) secondMx = nums[i];
}
return (*secondMx<=mx) ? mxId : -;
}
};
solution2:
class Solution {
public:
int dominantIndex(vector<int>& nums) {
int mx = INT_MIN, mxId = -;//err...
if(nums.size()<) return -;
for(int i=; i<nums.size(); i++)
{
if(nums[i]>mx) { mx = nums[i]; mxId = i; }
}
for(auto num:nums)
{
if(mx!=num && mx-num<num) return -;
}
return mxId;
}
};
参考
1. Leetcode_easy_747. Largest Number At Least Twice of Others;
2. Grandyang;
3. Discuss;
完
【Leetcode_easy】747. Largest Number At Least Twice of Others的更多相关文章
- 【LeetCode】747. Largest Number At Least Twice of Others 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 寻找两次最大值 排序 大顶堆 日期 题目地址:htt ...
- 【原创】leetCodeOj --- Largest Number 解题报告
原题地址: https://oj.leetcode.com/problems/largest-number/ 题目内容: Given a list of non negative integers, ...
- 【Leetcode】179. Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 【Leetcode_easy】976. Largest Perimeter Triangle
problem 976. Largest Perimeter Triangle solution: class Solution { public: int largestPerimeter(vect ...
- 【Leetcode_easy】949. Largest Time for Given Digits
problem 949. Largest Time for Given Digits solution: class Solution { public: string largestTimeFrom ...
- 【Leetcode_easy】812. Largest Triangle Area
problem 812. Largest Triangle Area solution: class Solution { public: double largestTriangleArea(vec ...
- 【Leetcode_easy】762. Prime Number of Set Bits in Binary Representation
problem 762. Prime Number of Set Bits in Binary Representation solution1: class Solution { public: i ...
- 【Leetcode_easy】693. Binary Number with Alternating Bits
problem 693. Binary Number with Alternating Bits solution1: class Solution { public: bool hasAlterna ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
随机推荐
- 多任务4---greenlet完成多任务
同yield一样 ,单线程,来回切换完成多任务,需要安装greenlet插件 pip install greenlet 代码: from greenlet import greenlet import ...
- ModbusRTU模式和结束符(转)
Modbus RTU模式的协议字段 起始位 设备地址 功能码 数据 CRC校验 结束符 至少3.5个字符 8bit 8bit N*8bit 16bit 至少3.5个字符 Modbus协议RTU模式要求 ...
- celery指定任务执行时间
有业务线提出需求:要求对于其流量,只能在0点到7点扫描. 对此,celery发送任务到队列时可以指定执行的时间. 当worker收到任务后,判断还未到执行时间,会存储在worker中,在到达时候后再执 ...
- SQL切分字符串成int和for xml path
切分字符 SqlServer切割字符串示例: --declare @StrDId nvarchar(2000) --set @StrDId='100,200,400,500,600' --转换ID,防 ...
- idea创建springmvc项目创部署成功,但访问controller层报错404
这个问题网上有很多解决问题,检查配置文件是否正确?controller注解是否扫描?项目启动是否成功等等. 访问报错404,而且后台也没错误,归根结底还是访问路径错了. 1.如图,idea配置tomc ...
- 爬虫(三):Requests库的基本使用
一:什么是Requests Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库如果你看过上篇文章关于urllib库的使用,你会发现 ...
- linux系列(二十二):tar命令
1.命令格式 tar[必要参数][选择参数][文件] 2.命令功能 用来压缩和解压文件.tar本身不具有压缩功能.他是调用压缩功能实现的 3.命令参数 必要参数: -A 新增压缩文件到已存在的压缩 - ...
- visual studio code(vs code) 编译、运行、调试程序(调用g++)
g++的安装过程忽略,记不清有没有"安装路径不能有空格"这种问题. 网上翻了几个博客,找到的配置文件在g++下都不能运行,遂折腾. 安装vscode与插件 插件为ms-vscode ...
- Python怎么测试异步接口
当业务处理比较耗时时, 接口一般会采用异步处理的方式, 这种异步处理的方式又叫Future模式. 一般流程 当你请求一个异步接口,接口会立刻返回你一个结果告诉你已经开始处理,结果中一般会包含一个任务i ...
- Python数据结构学习
列表 Python中列表是可变的,这是它区别于字符串和元组的最重要的特点,一句话概括即:列表可以修改,而字符串和元组不能. 以下是 Python 中列表的方法: 方法 描述 list.append(x ...