Problem:

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.

Summary:

找出整形数组中第三大的数,若存在第三大的数则返回该数,否则返回最大的数。

注意所谓的“大”为严格意义上的大于,大于等于的关系不成立。

Solution:

1. 首先将数组从大到小排序,然后从头遍历去掉重复数字,同时用一个指针记录当下去掉重复数字后的索引值。最后判断新数组中数字个数是否大于3即可。

 class Solution {
public:
static bool descending (int i, int j) { //自定义排序函数,此处应为static,否则报错
return i > j;
} int thirdMax(vector<int>& nums) {
int len = nums.size();
sort(nums.begin(), nums.end(), descending);
//sort(nums.begin(). nums.end(), greater<int>()); int j = ;
for (int i = ; i < len; i++) {
if(nums[i] < nums[i - ]) {
nums[j] = nums[i];
j++;
}
} return j > ? nums[] : nums[];
}
};

2. 用set维护当前的三个最大值,由于set有去重和自动从小到大排序的功能,可以再size大于3时去掉最小的,即当前的第四大数。

 class Solution {
public:
int thirdMax(vector<int>& nums) {
int len = nums.size();
set<int> s;
for (int i = ; i < len; i++) {
s.insert(nums[i]);
if (s.size() > ) {
s.erase(s.begin());
}
} return s.size()== ? *s.begin() : *s.rbegin();
}
};

参考:http://www.cnblogs.com/grandyang/p/5983113.html

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.

LeetCode 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 (第三大的数)

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

  3. 【leetcode】414. Third Maximum Number

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

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

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

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

  6. [LeetCode] 321. Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  7. LeetCode 321. Create Maximum Number

    原题链接在这里:https://leetcode.com/problems/create-maximum-number/description/ 题目: Given two arrays of len ...

  8. 【leetcode】1189. Maximum Number of Balloons

    题目如下: Given a string text, you want to use the characters of text to form as many instances of the w ...

  9. [LeetCode] 414. Third Maximum Number_Easy

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

随机推荐

  1. 我常用的VS技巧

    声明:开发工具使用的是VS2013 1.开发 包括编辑,代码补全等 1.1快速选择一行 第一种方式:鼠标停留在要选择的行上,三击. 第二种方式:鼠标停留在要选择的行上,按home键将鼠标停留在行首,按 ...

  2. phpstorm 激活服务器

    phpstorm 激活服务器 http://jetbrains.tencent.click/ (2016-09-19 可用) webstorm 激活服务器 http://owo.help(2016-0 ...

  3. SharePoint远程发布Solution

    1.在本地修改好代码,选择publish,将.wsp文件保存到本地: 2.将wsp文件上传到目标网站,停止原解决方案并删除,上传新解决方法并且激活: 停止原来的解决方案 上传解决方案 3.激活Solu ...

  4. JavaMeloay配置用于系统监控

    JavaMelody JavaMelody能够监控Java或Java EE应用程序服务器的运行状况,并以图标的方式显示:java内存.CPU使用情况.用户Session数量.JDBC连接数.http请 ...

  5. Ubuntu16.04/LinuxMint18安装openjdk-7-jdk

    LinuxMint18的安装源已经默认没有openjdk7了,所以要自己手动添加仓库,如下: sudo add-apt-repository ppa:openjdk-r/ppa sudo apt-ge ...

  6. [速记!vs调试技巧]

    当程序崩溃却又没有报错的时候,进入调试程序,断点处按Alt+7可以进入函数调用栈,甚至可以进入汇编栈,真的很有用,以后有时间学习汇编的话,估计这个功能会更加强大!

  7. 架构师养成记--3.synchronized细节问题

    一.synchronized有锁重入的特点,某个线程得到对象的锁后,再次请求此对象可以再次得到改对象的锁.如下示例,在method1中调用method2,在method2中调用method3,而met ...

  8. Alpha阶段第三次Scrum Meeting

    情况简述 Alpha阶段第三次Scrum Meeting 敏捷开发起始时间 2016/10/24 00:00 敏捷开发终止时间 2016/10/25 00:00 会议基本内容摘要 总结了之前学习的内容 ...

  9. centos 7.0 编译安装php 5.6.7

    编译安装php参考资料 MySQL PHP API http://dev.mysql.com/doc/apis-php/en/index.html nginx + php +mysql 最简单安装 官 ...

  10. System.Properties和System.getenv区别

    网上很多使用的是getProperties.说获得系统变量,但是其实不正确.getProperties中所谓的"system properties"其实是指"java s ...