【Leetcode_easy】747. Largest Number At Least Twice of Others
2024-09-01 05:01:52
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;
完
最新文章
- 《Entity Framework 6 Recipes》翻译系列 (5) -----第二章 实体数据建模基础之有载荷和无载荷的多对多关系建模
- 未能加载文件或程序集“Microsoft.SQLServer.DTSRuntimeWrap”或它的某一个依赖项。试图加载格式不正确的程序。
- linux上课
- MapReduce设置输出分隔符
- apache.http.client.HttpClient
- 什么办法可以替代distinct
- EF Code First 学习笔记:关系
- spring使用JdbcDaoSupport中封装的JdbcTemplate进行query
- 利用 Oracle EM 企业管理器 进行oracle SQL的优化(自动生成索引)
- 磁珠 磁环 双向二极管 TVS二极管
- Fedora24安装常用软件方法
- 文件搜索查找功能VC++
- C#用Zlib压缩或解压缩字节数组
- TCP协议随笔
- windows系统下输入法图标显示设置
- Eclipse格式化整个项目
- linux查看cpu个数,线程数及cpu型号
- HDOJ2013_蟠桃记
- 零基础学习python_with(34课)
- Spring Boot 系统要求