第三大的数

414、第三大的数

java 复制代码
class Solution {
    public int thirdMax(int[] nums) {

        
        Arrays.sort(nums);
        int temp=nums[0];
        int ans=nums[0];
        int count = 0;
        // if(nums.length<3){
        //     return nums[nums.length-1];
        // }
        // else {
            for(int i=nums.length-1;i>0;i--){
                if (nums[i]>nums[i-1]){
                    if(count>=2) break;
                    count++;
                    temp=nums[i-1];
                }
            }
        // }

        if(count>=2) ans=temp;
        else ans=nums[nums.length-1];

            return ans;
        }
 }
相关推荐
Yvonne爱编码3 分钟前
JAVA数据结构 DAY1-集合和时空复杂度
java·数据结构·python
iAkuya5 分钟前
(leetcode)力扣100 57电话号码的字母组合(回溯)
算法·leetcode·深度优先
m0_7369191018 分钟前
模板元编程性能分析
开发语言·c++·算法
pen-ai25 分钟前
【YOLO系列】 YOLOv1 目标检测算法原理详解
算法·yolo·目标检测
2301_765703141 小时前
C++中的职责链模式实战
开发语言·c++·算法
StandbyTime1 小时前
《算法笔记》学习记录-第一章
c++·算法·算法笔记
近津薪荼1 小时前
优选算法——双指针8(单调性)
数据结构·c++·学习·算法
松☆1 小时前
Dart 中的常用数据类型详解(含 String、数字类型、List、Map 与 dynamic) ------(2)
数据结构·list
格林威1 小时前
Baumer相机铆钉安装状态检测:判断铆接是否到位的 5 个核心算法,附 OpenCV+Halcon 的实战代码!
人工智能·opencv·算法·计算机视觉·视觉检测·工业相机·堡盟相机
星空露珠2 小时前
速算24点检测生成核心lua
开发语言·数据库·算法·游戏·lua