Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).

Example 1:

Input: [3, 2, 1]

Output: 1

Explanation: The third maximum is 1.

Example 2:

Input: [1, 2]

Output: 2

Explanation: The third maximum does not exist, so the maximum (2) is returned instead.

Example 3:

Input: [2, 2, 3, 1]

Output: 1

Explanation: Note that the third maximum here means the third maximum distinct number.
Both numbers with value 2 are both considered as second maximum.
思路:找出一个数组中第三大的元素,这里只是说integer,没有规定范围,因此应该考虑到int型的最大值和最小值,INT_MIN,INT_MAX,
自己的想法:(129ms)比较简单,使用额外的辅助空间,将不重复的元素排序
 int thirdMax(vector<int>& nums) {
sort(nums.begin(),nums.end());
vector<int> ans;
int i;
for(i=nums.size()-;i>=;--i)
{
if(find(ans.begin(),ans.end(),nums[i])==ans.end())//一开始自己的想法是进行排序,然后相邻元素是否相等,
ans.push_back(nums[i]);
}
if(ans.size()<)
return ans[];
return ans[];
}

或者这样:

优秀代码:(6ms)既然要找出第三大的元素值,则首先设定三个最小值,然后遍历所有的元素,找出前三大的元素值

 int thirdMax(vector<int>& nums) {
long long a, b, c;
a = b = c = LONG_MIN ;
for(auto num : nums){
if(num <= c || num ==b || num == a)
continue;
else
c = num;
if(c > b)
swap(b, c);
if(b > a)
swap(a, b);
}
return c == LONG_MIN ? a : c;
}

函数set下的代码:

 int thirdMax(vector<int>& nums) {
set<int> top;
for (auto i : nums) {
top.insert(i);
if (top.size() > ) top.erase(top.begin());
}
return top.size() == ? *top.begin() : *top.rbegin();
}

set参考链接:http://www.cnblogs.com/BeyondAnyTime/archive/2012/08/13/2636375.html

[Array]414. Third Maximum Number的更多相关文章

  1. C#版 - Leetcode 414. Third Maximum Number题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  2. 【leetcode】414. Third Maximum Number

    problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...

  3. LeetCode Array Easy 414. Third Maximum Number

    Description Given a non-empty array of integers, return the third maximum number in this array. If i ...

  4. LeetCode 414 Third Maximum Number

    Problem: Given a non-empty array of integers, return the third maximum number in this array. If it d ...

  5. LeetCode 414. Third Maximum Number (第三大的数)

    Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...

  6. 414. Third Maximum Number数组中第三大的数字

    [抄题]: Given a non-empty array of integers, return the third maximum number in this array. If it does ...

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

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

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

  9. 414. Third Maximum Number

    这个题有点坑啊..主要是自己蠢,以为 Integer.MIN_VALUE -1 == -2147483649 public class Solution { public int thirdMax(i ...

随机推荐

  1. Linux使用crontab定时执行Python脚本清理日志

    Linux中,周期执行的任务一般由crond这个守护进程来处理.cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间.crond的配置文件称为"crontab", ...

  2. 现代软件工程HW1:词频统计

    作业详细要求:http://www.cnblogs.com/denghp83/p/8627840.html 基本功能 1. 统计文件的字符数(只需要统计Ascii码,汉字不用考虑,换行符不用考虑,'\ ...

  3. Amazon S3和EBS的区别

  4. 03.MyBatis的核心配置文件SqlMapConfig.xml

    SqlMapConfig.xml中配置的内容和顺序如下: properties(属性) settings(全局配置参数) typeAliases(类型别名) typeHandlers(类型处理器) o ...

  5. leetcode-337-打家劫舍三*

    题目描述: 方法一:递归 # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): ...

  6. CycloneII lcell_comb 和 lcell_FF 的结构

    1,lcell_comb结构 2,lcell_FF结构 from : cycloneii_eda_fd.pdf

  7. thinkPHP 字段映射功能

    thinkPHP的字段映射功能可以让你在表单中隐藏真正的数据表字段,而不用担心放弃自动创建表单对象的功能,假设我们的User表里面有username和email字段,我们需要映射成另外的字段,定义方式 ...

  8. LUOGU P1613 跑路 (倍增floyd)

    解题思路 倍增$floyd$,首先设$f[i][j][k]$表示$i$这个点到$j$的距离能否为$2^k$,初值是如果x,y之间有边,那么$f[x][y][0]=1$.转移方程就是$f[i][j][t ...

  9. python中的多线程编程与暂停、播放音频的结合

    先给两个原文链接: https://blog.csdn.net/u013755307/article/details/19913655 https://www.cnblogs.com/scolia/p ...

  10. php中Sessions

    PHP Sessions  Session 中文译名叫做“会话”,其本来的含义是指有始有终的一系列动作/消息. PHP session 变量用于存储关于用户会话(session)的信息,或者更改用户会 ...