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;
    }
}
上一篇:CMS Day2


下一篇:0904csp七连day2比赛总结