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. ModernUI教程:独立显示器DPI感知

             独立显示器DPI感知,是在Windows 8.1中新增的特性,这个特性针对拥有多个显示器同时各个显示器的DPI设定又不同的人.对这个新特性做了优化支持的软件能够在一个高DPI的显示器 ...

  2. React.js常识

    1.React的特点: 简单.声明式-自动dom操作. React核心:组件,组件的设计目的:提高代码复用率,降低测试难度和代码复杂度. 提高代码复用率:组建将数据和逻辑进行封装,类似面向对象的类: ...

  3. epub电子书--目录结构介绍

    epub电子书简介 epub全称为Electronic Publication的缩写,意为:电子出版, epub于2007年9月成为国际数位出版论坛(IDPF)的正式标准,以取代旧的开放Open eB ...

  4. 配置windows 系统PHP系统环境变量

    1. 首先到php官网下载php-5.3.6-nts-Win32-VC9-x86.ZIP 解压到电脑硬盘.将文件解压到文件夹php5.3.6下载地址:http://www.php.net/downlo ...

  5. windows下MySQL 忘记初始密码

    一.windows下修改MySQL用户密码的方法:   1.关闭正在运行的MySQL服务:net stop mysql  或 在windows 任务管理器中结束 mysqld.exe 进程 或 在 管 ...

  6. Spring单例模式与线程安全

    问题背景 这段时间在做项目的时候,考虑到Spring中的bean默认是单例模式的,那么当多个线程调用同一个bean的时候就会存在线程安全问题.如果是Spring中bean的创建模式为非单例的,也就不存 ...

  7. JSVirtualMachine与JSContext

    JSVirtualMachine相当于进程: JSContext相当于线程:

  8. Protocol Framework - SNMP Tutorial

    30.4 Protocol Framework TCP/IP network management protocols2 divide the management problem into two ...

  9. Elasticsearch集群状态脚本及grafana监控面板导出的json文件

    脚本文件: #!/usr/bin/env python import datetime import time import urllib import json import urllib2 imp ...

  10. AFNetworking certificate AFNetworking 证书设置

    + (AFSecurityPolicy*)customSecurityPolicy { // /先导入证书 NSString *cerPath = [[NSBundle mainBundle] pat ...