Third Maximum Number——LeetCode⑬
//原题链接https://leetcode.com/problems/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: 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. - 思路分析
给一个非空的整数数组,返回第三大的数字,不足三个返回最大的数字;
时间复杂度必须为O(n)
比较简单,只要维持一个大小3的优先队列不断更新即可 - 源码附录
class Solution {
public int thirdMax(int[] nums) {
if(nums==null || nums.length==0)
{
return 0;
} PriorityQueue<Integer> priqueue = new PriorityQueue<Integer>();
Set<Integer> set = new HashSet<Integer>(); for(int i=0;i<nums.length;i++)
{
if(!priqueue.contains(nums[i]))
{
priqueue.add(nums[i]);
set.add(nums[i]);
if(priqueue.size()>3)
{
set.remove(priqueue.poll());
}
}
}
if(priqueue.size()<3)
{
while(priqueue.size()>1)
{
priqueue.poll();
}
}
return priqueue.peek();
}
}
Third Maximum Number——LeetCode⑬的更多相关文章
- [LeetCode] Third Maximum Number 第三大的数
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
- [LeetCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- 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 ...
- C#版 - Leetcode 414. Third Maximum Number题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 【leetcode】414. Third Maximum Number
problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...
- [LeetCode] 321. Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- LeetCode 321. Create Maximum Number
原题链接在这里:https://leetcode.com/problems/create-maximum-number/description/ 题目: Given two arrays of len ...
- LeetCode 414. 第三大的数(Third Maximum Number) 3
414. 第三大的数 414. Third Maximum Number 题目描述 给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是 O(n). 每 ...
- leetcode解题报告(15):Third Maximum Number
描述 Given a non-empty array of integers, return the third maximum number in this array. If it does no ...
- 【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 ...
随机推荐
- (自适应手机端)合同模板网站源码 合同范文类网站pbootcms模板
PbootCMS内核开发的网站模板,该模板适用于合同范文网站.合同模板网站等企业,当然其他行业也可以做,只需要把文字图片换成其他行业的即可: pc+wap,同一个后台,数据即时同步,简单适用!附带测试 ...
- Go红队开发—文件操作
目录 文件操作 创建目录 创建文件 获取File信息 文件重命名 删除文件 打开关闭文件 判断文件是否存在 判断文件是否有读取权限 复制文件 Read读取 ReadFull读取 ReadAtLeast ...
- Ubuntu下如何管理多个ssh密钥
Ubuntu下如何管理多个ssh密钥 前言 我一直在逃避这个问题,误以为我能够单纯地用一个 ssh 走天下. 好吧,现实是我不得不管理多个 ssh 做,那就写个博客总结一下吧. 查阅后发现前人已经 ...
- nginx 禁止直接访问目录或文件
前言 Nginx 默认是不允许列出整个目录的. 如需此功能,打开 nginx.conf 文件或你要启用目录浏览虚拟主机的配置文件,在 location server 或 http 段中加入 autoi ...
- 入口函数与包初始化:Go程序的执行次序
前言 我们可能经常会遇到这样一个问题:一个 Go 项目中有数十个 Go 包,每个包中又有若干常量.变量.各种函数和方法,那 Go 代码究竟是从哪里开始执行的呢?后续的执行顺序又是什么样的呢? 事实上, ...
- oracle修改用户密码的方法
Oracle用户名及默认密码 修改oracle用户的密码有以下方法: 普通用户 (1)通过alter user语法来进行修改 ,这也是最常见的方式: (2) 第二种方式,是通过password命令来修 ...
- Sa-Token v1.41.0 发布 🚀,来看看有没有令你心动的功能!
Sa-Token 是一个轻量级 Java 权限认证框架,主要解决:登录认证.权限认证.单点登录.OAuth2.0.微服务网关鉴权 等一系列权限相关问题. 目前最新版本 v1.41.0 已推送至 Mav ...
- BUUCTF---Morse
1.题目 -..../.----/-..../-..../-..../...--/--.../....-/-..../-..../--.../-.../...--/.----/--.../...--/ ...
- ZeroTier简单使用
在 CentOS 系统下,你可以使用以下命令行操作来管理 ZeroTier 网络和设备.首先,确保已经正确安装 ZeroTier 软件,你可以按照以下步骤进行安装: 安装 ZeroTier: Zero ...
- CoreOS 手动升级篇
说到升级...通常肯定会以下2个步骤: 检查是否有新版本. 下载和安装新版本. 在 CoreOS 中也一样,我们先来看下在 CoreOS 中对应的命令: # 检查是否有新版本 update_engin ...