LeetCode 414. Third Maximum Number
题目:
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相关推荐
- LeetCode | 414. Third Maximum Number
. 题目 Given an integer array nums, return the third distinct maximum number in this array. If the thi ...
- 414. Third Maximum Number
414. Third Maximum Number Given an array of integers, return the 3rd Maximum Number in this array, i ...
- 【Leetcode】2472. Maximum Number of Non-overlapping Palindrome Substrings
题目地址: https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/descript ...
- 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 ...
- 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 题目描述 思路 周赛代码 优化代码 ...
- LeetCode——1937. 扣分后的最大得分(Maximum Number of Points with Cost)[中等]——分析及代码(Java)
LeetCode--1937. 扣分后的最大得分[Maximum Number of Points with Cost][中等]--分析及代码[Java] 一.题目 二.分析及代码 1. 动态规划 ( ...
- C#LeetCode刷题之#414-第三大的数(Third Maximum Number)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3710 访问. 给定一个非空数组,返回此数组中第三大的数.如果不存 ...
- [Swift]LeetCode414. 第三大的数 | Third Maximum Number
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- 【leetcode】654. Maximum Binary Tree
题目如下: Given an integer array with no duplicates. A maximum tree building on this array is defined as ...
最新文章
- 异常处理与MiniDump详解(3) SEH(Structured Exception Handling)
- quickselect_QuickSelect:使用代码示例解释的快速选择算法
- Velocity笔记--使用Velocity获取动态Web项目名的问题
- 四旋翼———控制系统
- Linux-手动释放linux内存cache
- laravel 5.8 guzzle get 参数_Laravel速查表 Cache Cookie Request
- QML 实现图片帧渐隐渐显轮播
- 读取properties
- rabbitmq rpc
- U-Time巡回完美收官:精细化数据将主导未来运营趋势(数据应用篇)
- c语言小游戏百度云资源,c语言小游戏合集
- PassFab iPhone Unlocker安装方法
- 《UnityAPI.Rect矩阵》(Yanlz+Unity+SteamVR+云技术+5G+AI+VR云游戏+Rect+Contains+Overlaps+ToString+立钻哥哥++OK++)
- sap的pod确认_SAP POD 在途库存 SIT
- Heapsort 代码 学习笔记 阳春三月版
- IP前缀列表配置实验
- Python 3,一行代码处理各种时间转换,从此跟datetime,time模块说拜拜 ~ ~ 不收藏算我输!!!
- 初学AE影视后期的电脑硬件配置知识…
- igcse计算机科学知识点,常考的IGCSE生物知识点汇总 IG生物想拿A*这些考点你都会做了吗...
- LINUX系统编程__文件编程__LINUX库open等函数的应用
热门文章
- Linux显卡驱动|CUDA卸载和安装|pytorch安装
- 【mongodb】couldn't connect to server 127.0.0.1:27017 (127.0.0.1)
- 一句话了解 v-show 和 v-if 的区别
- pads铺铜不能开启drp_PADS中常见问题解决方案
- 新款微信壁纸小程序源码 自动采集小米
- QQ注册时间查询非常准确源码程序
- HTML在线颜色代码选取器源码
- WordPress主题Ashade2.0摄影展示 图片展示主题
- CSS3账号密码输入框动画模板
- npm安装时-S -D分别的意思