博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 414. Third Maximum Number
阅读量:4098 次
发布时间:2019-05-25

本文共 907 字,大约阅读时间需要 3 分钟。

题目:

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
& nums) { vector
sort;//保存nums排序后的vector sort.push_back(nums[0]);//存入nums第一个数 for (int i = 1; i
nums[i]){//若nums中的数比sort[j]小,则将nums存入当前位置之前,跳出循环 sort.insert(sort.begin() + j, nums[i]); break; } else if (sort[j]

转载地址:http://znmii.baihongyu.com/

你可能感兴趣的文章
Kafka
查看>>
9.1 为我们的角色划分权限
查看>>
维吉尼亚之加解密及破解
查看>>
DES加解密
查看>>
TCP/IP协议三次握手与四次握手流程解析
查看>>
PHP 扩展开发 : 编写一个hello world !
查看>>
inet_ntoa、 inet_aton、inet_addr
查看>>
用模板写单链表
查看>>
用模板写单链表
查看>>
链表各类操作详解
查看>>
C++实现 简单 单链表
查看>>
数据结构之单链表——C++模板类实现
查看>>
Linux的SOCKET编程 简单演示
查看>>
正则匹配函数
查看>>
Linux并发服务器编程之多线程并发服务器
查看>>
聊聊gcc参数中的-I, -L和-l
查看>>
[C++基础]034_C++模板编程里的主版本模板类、全特化、偏特化(C++ Type Traits)
查看>>
C语言内存检测
查看>>
Linux epoll模型
查看>>
Linux select TCP并发服务器与客户端编程
查看>>