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;

最新文章

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

热门文章

  1. Mybatis面向接口式编程
  2. Python Scrapy 爬虫框架实例
  3. tibco server keystore怎样使用多域名证书
  4. 区间连续长度的线段树——洛谷P2894 [USACO08FEB]酒店Hotel
  5. 解决vue多个路由共用一个页面的问题
  6. VTK 简单点云数据显示绘制
  7. 什么是vue的全家桶
  8. meshing-圆锥网格划分
  9. ES里设置索引中倒排列表仅仅存文档ID——采用docs存储后可以降低pos文件和cfs文件大小
  10. x2goserver 连接问题