其他分享
首页 > 其他分享> > Leetcode刷题——day2

Leetcode刷题——day2

作者:互联网

414. 第三大的数

class Solution {
    public int thirdMax(int[] nums) {
        int n = nums.length;
        long first, second, third;
        first = second = third = Long.MIN_VALUE;

        if(n == 1) return nums[0];
        if(n == 2) return Math.max(nums[0], nums[1]);

        for(int i = 0; i < n; i++) {
            if(nums[i] == first || nums[i] == second) continue;
            if(nums[i] > first) {
                third = second;
                second = first;
                first = nums[i];
                continue;
            }
            if(nums[i] > second) {
                third = second;
                second = nums[i];
                continue;
            }
            if(nums[i] > third) {
                third = nums[i];
            }
        }
        return third == Long.MIN_VALUE ? (int)first : (int)third;
    }
}

标签:return,third,nums,int,day2,second,first,Leetcode,刷题
来源: https://www.cnblogs.com/lykxbg/p/15212575.html