.

题目

Given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number.

Example 1:

Input: nums = [3,2,1]
Output: 1
Explanation:
The first distinct maximum is 3.
The second distinct maximum is 2.
The third distinct maximum is 1.

Example 2:

Input: nums = [1,2]
Output: 2
Explanation:
The first distinct maximum is 2.
The second distinct maximum is 1.
The third distinct maximum does not exist, so the maximum (2) is returned instead.

Example 3:

Input: nums = [2,2,3,1]
Output: 1
Explanation:
The first distinct maximum is 3.
The second distinct maximum is 2 (both 2’s are counted together since they have the same value).
The third distinct maximum is 1.

Constraints:

1 <= nums.length <= 10^4
-2^ 31 <= nums[i] <= 2^31 - 1

Follow up: Can you find an O(n) solution?

.

代码

class Solution {public:int thirdMax(vector<int>& nums) {set<int> s;for(int i = 0; i<nums.size(); i++)s.insert(nums[i]);int res;set<int>::iterator iter;if(s.size() < 3)for(iter = s.begin(); iter != s.end(); iter++) res = *iter;else{int j = s.size() - 3;for(iter = s.begin(); iter != s.end() && j >= 0; iter++, j--) res = *iter;}return res;}
};

.

LeetCode | 414. Third Maximum Number相关推荐

  1. LeetCode 414. Third Maximum Number

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

  2. 414. Third Maximum Number

    414. Third Maximum Number Given an array of integers, return the 3rd Maximum Number in this array, i ...

  3. 【Leetcode】2472. Maximum Number of Non-overlapping Palindrome Substrings

    题目地址: https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/descript ...

  4. leetcode 330. Patching Array | 1798. Maximum Number of Consecutive Values You Can Make

    1798. Maximum Number of Consecutive Values You Can Make | 1798. 你能构造出连续值的最大数目 https://leetcode.com/p ...

  5. LeetCode 第201次周赛 1546. Maximum Number of Non-Overlapping Subarrays With Sum Equals Target

    Leetcode 1546. Maximum Number of Non-Overlapping Subarrays With Sum Equals Target 题目描述 思路 周赛代码 优化代码 ...

  6. LeetCode——1937. 扣分后的最大得分(Maximum Number of Points with Cost)[中等]——分析及代码(Java)

    LeetCode--1937. 扣分后的最大得分[Maximum Number of Points with Cost][中等]--分析及代码[Java] 一.题目 二.分析及代码 1. 动态规划 ( ...

  7. C#LeetCode刷题之#414-第三大的数(Third Maximum Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3710 访问. 给定一个非空数组,返回此数组中第三大的数.如果不存 ...

  8. [Swift]LeetCode414. 第三大的数 | Third Maximum Number

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  9. 【leetcode】654. Maximum Binary Tree

    题目如下: Given an integer array with no duplicates. A maximum tree building on this array is defined as ...

最新文章

  1. SpringMVC之文件上传和文件下载
  2. 跌宕起伏之windows 7安装
  3. 数据结构-栈(C语言代码)
  4. 我能成为项目经理么?
  5. Cortex-M3 任务切换函数实例
  6. 让数字保持在整数范围内
  7. ajax该什么时候用
  8. 使用easyexcel导出时行高不自动调整的解决
  9. 数据结构 二叉树面试笔试编程题集
  10. Windows中安装Electron说明
  11. android自定义表格布局
  12. 莫烦pytorch学习笔记(三)
  13. 查看电脑CPU是否支持虚拟化
  14. shell编程——select语句东子破解
  15. win7下笔记本电脑给手机开热点
  16. 一筐梨子amp;一筐水果——协变性(covariant)
  17. prompt综述(截至2021.12.1)
  18. 华为是怎样研发的(4)——器件选型
  19. 如何制作一张完美的VR全景图片?
  20. 猫,路由器,宽带(光纤,ADSL),带宽的区别和联系

热门文章

  1. win10 无法启动network connections
  2. @Docker三剑客(三大编排工具):Compose、Machine和Swarm
  3. 安装ssd后不识别网卡_翻车了?安装固态硬盘后不识别的原因和解决方法
  4. 做数据分析的常用方法有哪些?
  5. 编写程序将hello-NOworld变成hello-world
  6. 简单账本-用完即走的微信小程序
  7. 将一个键盘输入的数字转化成中文输出(例如:输入1234567,输出:一百二拾三万四千五百六拾七)?
  8. Cesium 自定义箭头坐标轴 圆坐标轴(一)
  9. python中cumsum函数_Python pandas.DataFrame.cumsum函数方法的使用
  10. Mac操作系统下 命令行 cp命令的坑