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的更多相关文章

  1. 【LeetCode】747. Largest Number At Least Twice of Others 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 寻找两次最大值 排序 大顶堆 日期 题目地址:htt ...

  2. 【原创】leetCodeOj --- Largest Number 解题报告

    原题地址: https://oj.leetcode.com/problems/largest-number/ 题目内容: Given a list of non negative integers, ...

  3. 【Leetcode】179. Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  4. 【Leetcode_easy】976. Largest Perimeter Triangle

    problem 976. Largest Perimeter Triangle solution: class Solution { public: int largestPerimeter(vect ...

  5. 【Leetcode_easy】949. Largest Time for Given Digits

    problem 949. Largest Time for Given Digits solution: class Solution { public: string largestTimeFrom ...

  6. 【Leetcode_easy】812. Largest Triangle Area

    problem 812. Largest Triangle Area solution: class Solution { public: double largestTriangleArea(vec ...

  7. 【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 ...

  8. 【Leetcode_easy】693. Binary Number with Alternating Bits

    problem 693. Binary Number with Alternating Bits solution1: class Solution { public: bool hasAlterna ...

  9. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

随机推荐

  1. idea远程debug调试阿里云ECS

    1.首先远程服务器的代码跟本地项目代码应该完全一致,否则会出现debug混乱现象,亲测. 2.config如图: ①命名可以省略②复制这个地址③输入远程ip和自定义且未被占用的端口号xxxx 3.开放 ...

  2. Linux下干净卸载mysql

    1.首先查看mysql的安装情况 rpm -qa|grep -i mysql 显示之前安装了: MySQL-client-5.5.25a-1.rhel5 MySQL-server-5.5.25a-1. ...

  3. python - redis 的使用

    1.redis连接 redis提供两个类Redis和StrictRedis用于实现Redis的命令,StrictRedis用于实现大部分官方的命令,并使用官方的语法和命令,Redis是StrictRe ...

  4. js手机点击图片放大

    点击每个图片获取到对应的img的url链接,再把链接给一个空img以此来实现 最终效果:

  5. Java处理小数点后几位

    //方式一: //四舍五入 double f = 111231.5585; BigDecimal b = new BigDecimal(f); , BigDecimal.ROUND_HALF_UP). ...

  6. bash: sz: command not found

    Linux系统中如果没有安装 lrzsz这个包,就会报rz.sz命令找不到,安装即可解决. 命令: yum install lrzsz 效果图:

  7. 洛谷 P3955 图书管理员 题解

    每日一题 day12 打卡 Analysis 模拟+快速幂 先把图书的编码存起来排序,保证第一个找到的就是最小的.如果要求一个数后x位,就将这个数模10的x次方,同理,我们可以通过这个规律来判断后缀. ...

  8. elasticsearch 集群管理(集群规划、集群搭建、集群管理)

    一.集群规划 搭建一个集群我们需要考虑如下几个问题: 1. 我们需要多大规模的集群? 2. 集群中的节点角色如何分配? 3. 如何避免脑裂问题? 4. 索引应该设置多少个分片? 5. 分片应该设置几个 ...

  9. 带你了解HTTP协议(二)

    同样的,本文篇幅也比较长,先来一张思维导图,带大家过一遍.   一图看完本文 一. 计算机网络体系结构分层   计算机网络体系结构分层   计算机网络体系结构分层 不难看出,TCP/IP 与 OSI ...

  10. swoole入门到实战打造高性能赛事直播平台☆

    ​ 第1章 课程介绍 本章主要是介绍了swoole的一些特性,以及使用场景,并且分享了swoole在其他公司的一些案例,最后重点讲解了swoole学习的一些准备工作. 第2章 PHP 7 源码安装 本 ...