题目:

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: 1Explanation: The third maximum is 1.

Example 2:

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

Example 3:

Input: [2, 2, 3, 1]Output: 1Explanation: Note that the third maximum here means the third maximum distinct number.
Both numbers with value 2 are both considered as second maximum.

思路:给定一个vector,找到vector中第三大的不重复的数,若不存在,则返回最大的数。

代码:

class Solution {
public:int thirdMax(vector<int>& nums) {vector<int> sort;//保存nums排序后的vectorsort.push_back(nums[0]);//存入nums第一个数for (int i = 1; i<nums.size(); ++i){//将nums中的数遍历存入sort中for (int j = 0; j<sort.size(); ++j){if (sort[j] == nums[i]){//若相等,即重复,break不执行break;}if (sort[j]>nums[i]){//若nums中的数比sort[j]小,则将nums存入当前位置之前,跳出循环sort.insert(sort.begin() + j, nums[i]);break;}else if (sort[j]<nums[i] && j == sort.size() - 1){//如果比所有的数都大,则在末尾push_backsort.push_back(nums[i]);}}}if (sort.size()<3){return *(sort.end() - 1);//sort长度小于3,即没有第三大的数,返回最大的}else{return *(sort.end() - 3);//有第三大的,返回第三大的}}
};

LeetCode 414. Third Maximum Number相关推荐

  1. LeetCode | 414. Third Maximum Number

    . 题目 Given an integer array nums, return the third distinct maximum number in this array. If the thi ...

  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. 异常处理与MiniDump详解(3) SEH(Structured Exception Handling)
  2. quickselect_QuickSelect:使用代码示例解释的快速选择算法
  3. Velocity笔记--使用Velocity获取动态Web项目名的问题
  4. 四旋翼———控制系统
  5. Linux-手动释放linux内存cache
  6. laravel 5.8 guzzle get 参数_Laravel速查表 Cache Cookie Request
  7. QML 实现图片帧渐隐渐显轮播
  8. 读取properties
  9. rabbitmq rpc
  10. U-Time巡回完美收官:精细化数据将主导未来运营趋势(数据应用篇)
  11. c语言小游戏百度云资源,c语言小游戏合集
  12. PassFab iPhone Unlocker安装方法
  13. 《UnityAPI.Rect矩阵》(Yanlz+Unity+SteamVR+云技术+5G+AI+VR云游戏+Rect+Contains+Overlaps+ToString+立钻哥哥++OK++)
  14. sap的pod确认_SAP POD 在途库存 SIT
  15. Heapsort 代码 学习笔记 阳春三月版
  16. IP前缀列表配置实验
  17. Python 3,一行代码处理各种时间转换,从此跟datetime,time模块说拜拜 ~ ~ 不收藏算我输!!!
  18. 初学AE影视后期的电脑硬件配置知识…
  19. igcse计算机科学知识点,常考的IGCSE生物知识点汇总 IG生物想拿A*这些考点你都会做了吗...
  20. LINUX系统编程__文件编程__LINUX库open等函数的应用

热门文章

  1. Linux显卡驱动|CUDA卸载和安装|pytorch安装
  2. 【mongodb】couldn't connect to server 127.0.0.1:27017 (127.0.0.1)
  3. 一句话了解 v-show 和 v-if 的区别
  4. pads铺铜不能开启drp_PADS中常见问题解决方案
  5. 新款微信壁纸小程序源码 自动采集小米
  6. QQ注册时间查询非常准确源码程序
  7. HTML在线颜色代码选取器源码
  8. WordPress主题Ashade2.0摄影展示 图片展示主题
  9. CSS3账号密码输入框动画模板
  10. npm安装时-S -D分别的意思