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 ...
随机推荐
- ARC101E题解
前言 此片题解大致按照笔者做题思路进行讲解. 简要题意 有一棵树,树上有偶数个节点.你需要给这些点两两配对,一组已经配对的点会将两点之间的树边进行一次覆盖.一组合法方案需要满足树上所有边都被覆盖至少一 ...
- 记录:tinyrenderer---1.2 Rasterizing the boundary
光栅化三角形 Scanline rendering(扫描线渲染),一个老式的算法 按y轴坐标进行排序,我这里采取降序,ay > by > cy 同时光栅化三角形的左右两边 绘制水平线段,连 ...
- Vue2/Vue3 项目生产环境开启 vue devtools 插件线上调试 vue 组件
说到 vue 项目的调试工具,必然少不了 "vue devtools 插件",此插件就像"手术刀"一样,是开发环境下的一个利器,生产环境一般情况没办法使用. 要 ...
- linux部署go项目
直接部署: 1.将程序所需要的文件如配置文件和生成的可执行文件拷贝到linux中 2.直接执行./main命令,启动程序 (main是go编译生成的可执行文件) 如果报Permission denie ...
- linux查看redis版本
1. redis-server --version 和 redis-server -v 2.redis-cli --version 和 redis-cli -v 如果报redis-server或red ...
- 【抓包】Fidder Script自动修改包
Fiddler Script的本质是用JScript.NET编写的一个脚本文件CustomRules.js 但是它的语法很像C#但又有些不一样,比如不能使用@符号 通过修改CustomRules.js ...
- .NET 生成PDF文件
1.网上检索N种解决方案 QuestPDF:简单方便实用,文档也相对来说全,但是开源协议,当企业规模大的100W美金需要收费,未来存在潜在版权问题. itext7:感觉实用偏复杂,项目类库引用复杂,不 ...
- 准确理解 JS 的 ++ 运算符
对于刚开始接触前端开发的朋友们来说,可能地一个令人苦恼的问题是关于运算符 ++ 的计算,特别是它还有前置与后置的区别.当它们和一堆运算在一起的时候,常常令人头晕目眩! 我经常性地称它是一个***难人的 ...
- MySQL基础架构-架构详解
mysql基础架构图 架构详解 连接器部分 整体功能 建立连接 维持管理连接 校验用户名密码,查询权限 最佳实践 不要在命令行客户端中明文输入密码 -p 中不要使用明文密码 修改权限,需要重新建立连接 ...
- Readers and Writers JSON Framework(2)
我们关心json的读写.特别在datasnap中,关于使用stream更是显得重要.其实轮子都帮你做好了,你不知道整经再研究就是一个悲哀.除非你要研究. 回正题: 处理json有二套框架. JSON ...