[Array]414. Third Maximum Number
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的更多相关文章
- C#版 - Leetcode 414. Third Maximum Number题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 【leetcode】414. Third Maximum Number
problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...
- 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 ...
- LeetCode 414 Third Maximum Number
Problem: Given a non-empty array of integers, return the third maximum number in this array. If it d ...
- 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 ...
- 414. Third Maximum Number数组中第三大的数字
[抄题]: Given a non-empty array of integers, return the third maximum number in this array. If it does ...
- 【LeetCode】414. Third Maximum Number 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 替换最大值数组 使用set 三个变量 日期 题目地址 ...
- 【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 ...
- 414. Third Maximum Number
这个题有点坑啊..主要是自己蠢,以为 Integer.MIN_VALUE -1 == -2147483649 public class Solution { public int thirdMax(i ...
随机推荐
- 使用CEfSharp之旅(4)cefsharp 调用F12
原文:使用CEfSharp之旅(4)cefsharp 调用F12 版权声明:本文为博主原创文章,未经博主允许不得转载.可点击关注博主 ,不明白的进群191065815 我的群里问 https://bl ...
- wpf问题集锦
一.今天用vs2013新建wpf程序,项目名称命名为MainWindow,一启动就出现错误:类型“MainWindow.MainWindow”中不存在类型名称"App".博主顿时很 ...
- 透视jvm之垃圾回收
JVM是JAVA世界的核心,了解它有助于我们更好调试,调优和开发程序,最近散仙在看JAVA特种兵一书,看完觉得,作者写的内容还是挺不错,大家感兴趣的,也可以购买本温故而知新下. 在JVM中,我们经常提 ...
- MFC入门--显示静态图片及调用本地软件
MFC是微软开发的基础类库,主要用来开发图形界面应用程序,在学习中,我们要验证算法好坏,一般需要对结果进行可视化. OpenCV是计算机视觉中的开源算法库,集成了很多先进算法,现在想将MFC与Open ...
- 模块介绍/time/os...
本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configpars ...
- Spring Cloud Alibaba发布第二个版本,Spring 发来贺电
还是熟悉的面孔,还是熟悉的味道,不同的是,这次的配方升级了. 今年10月底,Spring Cloud联合创始人Spencer Gibb在Spring官网的博客页面宣布:阿里巴巴开源 Spring Cl ...
- day05 创建用户过程、文件夹,文件等权限修改等
linux创建用户时修改到的文件 /etc/passwd //存放用户信息 /etc/shadow //存放用户密码信息 /etc/group //存放用户组信息 /etc/gshadow //存放组 ...
- centos6 nginx 配置本地https访问
安装准备 yum install openssl openssl-devel 生成文件 cd /usr/local/nginx/conf # 生成密钥privkey.pem: openssl genr ...
- JS的高阶函数
JavaScript的函数其实都指向某个变量.既然变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数. 这就是最简单的高阶函数啦 functio ...
- python随机数(转载)
随机生成 0 到 1 之间的浮点数 random.random() 方法会返回 [0.0, 1.0) 之间的浮点数,注意,这是一个左闭右开的区间,随机数可能会是 0 但不可能为 1 . 随机生成 a ...