problem

414. Third Maximum Number

solution

思路:用三个变量first, second, third来分别保存第一大、第二大和第三大的数,然后遍历数组。

class Solution {
public:
int thirdMax(vector<int>& nums) {
//1.LONG_MIN;
long first = LONG_MIN, second = LONG_MIN, third = LONG_MIN;//
for(auto a:nums)
{
if(a>first)
{
if(second!=LONG_MIN) third = second;//判断.
if(first!=LONG_MIN) second = first;
first = a;
}
else if(a<first &&a>second && a!=first)
{
if(second!=LONG_MIN) third = second;
second = a;
}
else if(a<second && a>third && a!=first && a!=second)
{
third = a;
}
}
return (third==LONG_MIN)?first:third; }
};

注意:

1. 数据类型对应的最小值表示;

2. 赋值时需要判断是否为最小值;

参考

1. Leetcode_414. Third Maximum Number;

2.[leetcode] 414. Third Maximum Number 解题报告;

【leetcode】414. Third Maximum Number的更多相关文章

  1. 【LeetCode】414. Third Maximum Number 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 替换最大值数组 使用set 三个变量 日期 题目地址 ...

  2. 【LeetCode】9、Palindrome Number(回文数)

    题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...

  3. 【leetcode】668. Kth Smallest Number in Multiplication Table

    题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...

  4. 【leetcode❤python】 414. Third Maximum Number

    #-*- coding: UTF-8 -*- #l1 = ['1','3','2','3','2','1','1']#l2 = sorted(sorted(set(l1),key=l1.index,r ...

  5. 【LeetCode】1133. Largest Unique Number 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 桶排序 日期 题目地址:https://leetcod ...

  6. 【LeetCode】405. Convert a Number to Hexadecimal 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  7. 【LeetCode】754. Reach a Number 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学 日期 题目地址:https://leetcod ...

  8. 【LeetCode】611. Valid Triangle Number 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/valid-tri ...

  9. 【leetcode】Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

随机推荐

  1. Html ul、dl、ol 标签

    Html ul.dl.ol 标签 <html> <body> <!-- ul 标签指定字符断点,左边带一个“·”点--> <ul> <!-- li ...

  2. oracle flashback data archive闪回数据归档天坑之XID重用导致闪回查询数据重复

    我们有个系统使用了Oracle flashback data archive闪回数据归档特性来作为基于时间点的恢复机制,在频繁插入.更新期间发现SYS_FBA_HIST_NNNN表中的XID被两个事务 ...

  3. docker启动容器报错: could not synchronise with container process: not a directory

    错误现象 在运行容器时,出现以下错误 [root@localhost test]# docker run -it -d -v $PWD/test.txt:/mydir mytest fd44cdc55 ...

  4. VS2015 scanf 函数报错 error C4996: 'scanf'

    错误提示:error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. ...

  5. 尚硅谷面试第一季-13git分支相关命令

    课堂重点:分支相关命令 实际应用-工作流程 实操命令及运行结果: 创建master分支并提交 git init git add . git commit -m "V1.0" git ...

  6. java基础语法2.

    第二章 2.1 class文件的生成 java文件为源代码文件 class为程序. class文件实时修改. eclipse自动生成. project下面clean. 2.2 jar文件 如何将有用的 ...

  7. Always clear download 下载 谷歌浏览器插件

    由于该博文不支持上传压缩包,因此,如有需要always clear download插件的可点击此链接在百度网盘上下载https://pan.baidu.com/s/13wWchis3iKqXkIA5 ...

  8. asp.net 后台 get,post请求

    //Post请求 public static string Post(string url,string obj=null) { string param = (obj);//参数 byte[] bs ...

  9. MySQL 5.6容器使用自定义配置文件的权限问题

    提出问题: 在使用Rancher2.0.2部署一个mysql deployment时,我们会发现,如果只设置/var/lib/mysql数据目录时,mysql容器(pod)能够正常启动,一旦数据目录和 ...

  10. [easyUI] autocomplete 简单自动完成以及ajax从服务器端完成

    通过id取input标签对象,调用autocomplete方法 <script> var sources = [ "ActionScript", "Apple ...