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. Docker-compose

    docker-compose:未找到命令 安装: 须切到root用户: curl -L https://github.com/docker/compose/releases/download/1.7. ...

  2. localStorage与sessionStorage 的区别

    通过一枚页面计数器来区别localStorage与sessionStorage. 通过一个计数变量pageconut,每刷新页面,增加的是localStorage的数量,而sessionStorage ...

  3. MySQL安装步骤

    MySQL安装步骤 1. 下载MySQL Community Server 5.6.21,注意选择系统类型(32位/64位) 2. 解压MySQL压缩包 将以下载的MySQL压缩包解压到自定义目录下. ...

  4. 【USACO 2.3】The Longest Prefix

    题意: 给你一个少于200000的字符串,求最长的可以划分为给定词典里的单词的前缀. 题解: dp[i]表示第i位结尾的前缀是否可行,然后枚举每一位如果dp[i-1]==1,枚举所有单词,匹配成功的单 ...

  5. sphinx应用

     sphinx调用原理 只需要提交要查询,sphinx将返回唯一的id号  API调用 1.创建连接 $sphinx = new SphinxClient(); $sphinx->SetServ ...

  6. 在ubuntu/deepin/mint等系统中使用命令删除文件或文件夹

    此命令操作需谨慎: sudo rm -rf 文件夹路径(或文件路径)  

  7. IntelliJ IDEA 常用设置讲解

    说明 IntelliJ IDEA 有很多人性化的设置我们必须单独拿出来讲解,也因为这些人性化的设置让我们这些 IntelliJ IDEA 死忠粉更加死心塌地使用它和分享它. 常用设置 IntelliJ ...

  8. 用Python建立最简单的web服务器

    利用Python自带的包可以建立简单的web服务器.在DOS里cd到准备做服务器根目录的路径下,输入命令: python -m Web服务器模块 [端口号,默认8000] 例如: python -m ...

  9. SDWebImage使用及原理

    第一步,下载SDWebImage,导入工程.github托管地址https://github.com/rs/SDWebImage 第二步,在需要的地方导入头文件 1 #import "UII ...

  10. <<< ajaxfileupload介绍

    ajaxfileupload,jquery的一个异步上传插件,使用此插件你可以不用建立form,他会自动生成表单,且自动设置好enctype="multipart/form-data&quo ...