//原题链接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⑬的更多相关文章

  1. [LeetCode] Third Maximum Number 第三大的数

    Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...

  2. [LeetCode] Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  3. 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 ...

  4. C#版 - Leetcode 414. Third Maximum Number题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  5. 【leetcode】414. Third Maximum Number

    problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...

  6. [LeetCode] 321. Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  7. LeetCode 321. Create Maximum Number

    原题链接在这里:https://leetcode.com/problems/create-maximum-number/description/ 题目: Given two arrays of len ...

  8. LeetCode 414. 第三大的数(Third Maximum Number) 3

    414. 第三大的数 414. Third Maximum Number 题目描述 给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是 O(n). 每 ...

  9. leetcode解题报告(15):Third Maximum Number

    描述 Given a non-empty array of integers, return the third maximum number in this array. If it does no ...

  10. 【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 ...

随机推荐

  1. MySQL - [18] mysql中关于cascade的用法

    drop database语句用于删除数据库.但如果想要删除一个数据库并且还要删除所有依赖于该数据库的存储过程.函数等,可以使用cascade关键字.drop database test cascad ...

  2. 单元测试三部曲-AAA模式

    AAA 指的是 "Arrange, Act, Assert",这是一种通用的单元测试模式. 在测试方法中, 1.首先对测试对象进行准备(Arrange), 2.然后调用要测试的方法 ...

  3. Ai 系列 —— DeepSeek 初步介绍

    DeepSeek 初步使用介绍 背景 Ai 正在慢慢在改变我们的生活,比如老一辈可能已经在用豆包(字节跳动推出的AI聊天机器人) 前端开发,某些公司内部已在使用图生文(设计稿生成前端代码) 网上也有许 ...

  4. SpringBoot三种获取Request和Response的方法

    通过静态方法获取,你也可以封装一个静态方法出来 @GetMapping(value = "") public String center() { ServletRequestAtt ...

  5. sql server 2017 STRING_AGG() 替代方案

    SELECT @StuId='"'+STRING_AGG(Id,'","')+'"'FROM( SELECT 'a'+cast(Id as varchar) I ...

  6. 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)

    FRP 是 Github 上开源的一款内网穿透工具,点击前往项目地址,该项目分为 frps 服务端和 frpc 客户端,通过在拥有公网 IP 的服务器上搭建服务端,然后在被穿透的机器上安装客户端,配置 ...

  7. mongodb 数据库操作——备份 还原 导出 导入

    mongodump备份数据库 命令格式 mongodump -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -o 文件存在路径 如果没有用户,可以去掉-u和-p. 如果导出本机 ...

  8. 无法解析@NotBlank

    当碰到无法解析的时候,一般都是地址写错了,找不到相应的路劲 我是全局能搜到这个包@NotBlank,在jakarta.validation-api包里面,但是我网上搜https://www.cnblo ...

  9. 堆排序(标准版)(NB)

    博客地址:https://www.cnblogs.com/zylyehuo/ # _*_coding:utf-8_*_ import random def sift(li, low, high): # ...

  10. Tomcat之——宕机自动重启和每日定时启动tomcat

    在项目后期维护中会遇到这样的情况,tomcat在内存溢出的时候就出现死机的情况和遇到长时间不响应,需要人工手动关闭和重启服务,针对这样的突发情况,希望程序能自动处理问题而不需要人工关于,所以才有了目前 ...