安全第一天

雷亚荣

关注

阅读 66

2023-07-27

414、第三大的数
在这里插入图片描述

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;
        }
 }


精彩评论(0)

0 0 举报