首页 > 其他分享 >leetcode-414-easy

leetcode-414-easy

时间:2023-01-04 19:34:47浏览次数:41  
标签:map set third nums distinct maximum 414 easy leetcode

Third Maximum Number

Given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number.

Example 1:

Input: nums = [3,2,1]
Output: 1
Explanation:
The first distinct maximum is 3.
The second distinct maximum is 2.
The third distinct maximum is 1.
Example 2:

Input: nums = [1,2]
Output: 2
Explanation:
The first distinct maximum is 2.
The second distinct maximum is 1.
The third distinct maximum does not exist, so the maximum (2) is returned instead.
Example 3:

Input: nums = [2,2,3,1]
Output: 1
Explanation:
The first distinct maximum is 3.
The second distinct maximum is 2 (both 2's are counted together since they have the same value).
The third distinct maximum is 1.
Constraints:

1 <= nums.length <= 104
-231 <= nums[i] <= 231 - 1
Follow up: Can you find an O(n) solution?

思路一:用有序 map 记录所有值,在去重的同时做好了排序

    public int thirdMax(int[] nums) {

        Map<Integer, Integer> map = new TreeMap<>();
        Arrays.sort(nums);

        for (int num : nums) {
            map.compute(num, (k, v) -> v == null ? 1 : v + 1);
        }

        Set<Integer> set = map.keySet();
        if (set.size() < 3) {
            return set.toArray(new Integer[]{})[set.size() - 1];
        } else {
            return set.toArray(new Integer[]{})[set.size() - 3];
        }
    }

标签:map,set,third,nums,distinct,maximum,414,easy,leetcode
From: https://www.cnblogs.com/iyiluo/p/17025799.html

相关文章

  • leetcode-563-easy
    BinaryTreeTiltGiventherootofabinarytree,returnthesumofeverytreenode'stilt.Thetiltofatreenodeistheabsolutedifferencebetweenthesum......
  • leetcode-349-easy
    IntersectionofTwoArraysGiventwointegerarraysnums1andnums2,returnanarrayoftheirintersection.Eachelementintheresultmustbeuniqueandyoum......
  • leetcode-350-easy
    IntersectionofTwoArraysIIGiventwointegerarraysnums1andnums2,returnanarrayoftheirintersection.Eachelementintheresultmustappearasmany......
  • leetcode-367-easy
    ValidPerfectSquareGivenapositiveintegernum,returntrueifnumisaperfectsquareorfalseotherwise.Aperfectsquareisanintegerthatisthesquar......
  • 【转载】SRS、EasyDarwin、ZLMediaKit、Monibuca对比分析
    声明转自liuzhen007的《SRS、EasyDarwin、ZLMediaKit、Monibuca对比分析》作者:liuzhen007链接:https://juejin.cn/post/6926739029496954888来源:稀土掘金著作权归作......
  • 【栈】LeetCode 20. 有效的括号
    题目链接20.有效的括号思路碰见左括号就入栈,碰见右括号就和检查栈顶括号是否配队。遍历完后还要检查栈是否为空,确定括号数量是合法的。代码classSolution{publ......
  • 【逆波兰表达式】【栈】LeetCode 150. 逆波兰表达式求值
    题目链接150.逆波兰表达式求值思路从左到右遍历tokens遇到数字便放入栈中,遇到运算符便弹出栈顶的两个数字进行运算。代码classSolution{publicintevalRPN(......
  • EasyAR4.0稀疏空间地图室内导航
    现有的AR室内导航,一种方案是利用运动跟踪实现,但是偏移较大。比较靠谱或者说能满足商业使用的还是稀疏空间地图。(ARCore管叫云锚点)实现效果如下:EasyAR稀疏云地图室内导航......
  • EasyAR4.0使用说明(五)----3D物体跟踪
    3D物体跟踪总体上是和平面图像跟踪差不多的,设置,包括程序控制,识别多个对象。区别只是目标对象的不同。总体说明3D物体跟踪对3D物体的纹理,也就是表面的图案的丰富程度是有要求......
  • 《Unity3D平台AR开发快速上手--基于EasyAR4.0》随书资源和相关说明
    新手《Unity3D平台AR开发快速上手–基于EasyAR4.0》上市了,现在京东和淘宝都有卖。书分为2个部分,第一部分是EasyAR4.0基础内容和使用,第二部分是利用EasyAR的稀疏空间地图做室......