【leetcode】414. Third Maximum Number
problem
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的更多相关文章
- 【LeetCode】414. Third Maximum Number 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 替换最大值数组 使用set 三个变量 日期 题目地址 ...
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- 【leetcode】668. Kth Smallest Number in Multiplication Table
题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...
- 【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 ...
- 【LeetCode】1133. Largest Unique Number 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 桶排序 日期 题目地址:https://leetcod ...
- 【LeetCode】405. Convert a Number to Hexadecimal 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】754. Reach a Number 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学 日期 题目地址:https://leetcod ...
- 【LeetCode】611. Valid Triangle Number 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/valid-tri ...
- 【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 ...
随机推荐
- Python IO内核交互了解
注:Unix \ Linux 环境下的network IO 用户空间与内核空间 现在操作系统都是采用虚拟存储器,那么对32位操作系统而言,它的寻址空间(虚拟存储空间)为4G(2的32次方).操作系 ...
- How use Nmon and "Java Nmon Analyzer" for Monitor Linux Performance
Nmon is a resource monitoring tools which can monitor CPU, Memory, Disks, Network and even Filesyst ...
- eclipse软件仿真操作
1.编写程序代码(以SDRAM为例) 1.1 编写head.s汇编文件 .equ SDRAM_BASE, 0x30000000 .equ MEM_CTL_BASE, 0x48000000 .text ...
- 《温故而知新》JAVA基础六
多态(父子类之间) 对象的多种形态 引用多态 父类的引用可以指向本类对象 父类的引用可以指向子类的对象 方法的多态 创建本类对象时候,调用的方法是本类方法 创建子类对象时候,调用的方法为子类重写的方法 ...
- _mount_allowed
该表配置可以坐骑的使用区域,可能需要修改spell.dbc,允许在室内等特殊区域使用坐骑技能
- win8外包公司——技术分享:参数传递
页面之间传递参数 windows phone 的参数传递和web 差不多.用“?”号传递 多个参数的时候用 “&”做分隔. 我接着昨天的项目继续添加一个FourPage.xaml 在昨天的Th ...
- IOS 下载app
---------------------------------------------------------------------------------------------------- ...
- 无头浏览器phantomJS
selenium: 有头浏览器的代表(selenium+python也可实现静默运行 引入python的一个包,包叫:虚拟屏幕pyvirtualdisplay) PhantomJS : 无头浏览器的代 ...
- JavaScript中的label语句,及应用
label语句:可以在代码中添加标签,以便将来使用. 语法:label : statement 示例: start: for(var i=0;i<10;i++){ console.log(i); ...
- 在500jsp错误页面获取错误信息
自定义异常发生时的错误处理页面: 1) 只要定义page指示元素的errorPage属性就可以指定当前页面发生异常时应该交给哪个页面进行处理,例如:<%@page errorPage=" ...