[LeetCode 414] Third Maximum Number

  • Question
    Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).
    Example 1:
    Input: [3, 2, 1]
    Output: 1
    Explanation: The third maximum is 1.
    Example 2:
    Input: [1, 2]
    Output: 2
    Explanation: The third maximum does not exist, so the maximum (2) is returned instead.
    Example 3:
    Input: [2, 2, 3, 1]
    Output: 1
    Explanation:
    Note that the third maximum here means the third maximum distinct number.Both numbers with value 2 are both considered as second maximum.

  • 思路
    题目对于时间要求 O(n), 只能遍历一次数组, 在遍历中只要维护 first_max, second_max, third_max就好了。用 flash来标识三个最大值的赋值情况:


flash=0     应该给 fist_max 赋值,
flash=1     应该给 second_max 赋值
flash=2     应该给 third_max赋值
flash=3     更新三个值
  • 代码:
class Solution {
public:
    int thirdMax(vector<int>& nums) {
        int first_max=0, second_max=0, third_max=0;
        int flash=0;
        for(auto &a:nums){
            switch(flash){
                case 0:
                    first_max=a;
                    ++flash;
                    break;
                case 1:
                    if(a > first_max){
                      second_max=first_max;
                      first_max=a;
                      ++flash;
                     }
                    else if(a<first_max){
                        second_max=a;
                        ++flash;
                    }
                    break;
                case 2: 
                    if(a> first_max){
                        third_max=second_max;
                        second_max=first_max;
                        first_max=a;
                        ++flash;
                    }
                    else if(first_max > a && a >     second_max){
                        third_max=second_max;
                        second_max=a;
                        ++flash;
                    }
                    else if(a<second_max){
                        third_max=a;
                        ++flash;
                    }
                    break;
                default:
                    if(a>first_max){
                        third_max=second_max;
                        second_max=first_max;
                        first_max=a;
                    }
                    else if(first_max>a && a>second_max){
                        third_max=second_max;
                        second_max=a;
                    }
                    else if(second_max>a && a>third_max) 
                    third_max=a;
                    break;
            }
        }
        return flash>2? third_max:first_max;
    }

};

这个代码的可读性很差,我在网上参考其他人的代码后优化如下:

class Solution {
public:
   int thirdMax(vector<int>& nums) {
    int first_max = nums[0], second_max = INT_MIN, third_max = INT_MIN;
    int flash = 0;
    for (auto &a : nums) {
        if ((flash>2 && a<third_max) || (flash>=0 && a == first_max) || 
            (flash>=1 && a == second_max) || (flash>=2 && a == third_max)) continue;
        ++flash;
        if (a>first_max) {
            third_max = second_max;
            second_max = first_max;
            first_max = a;
        }
        else if (a>second_max){
            third_max = second_max;
            second_max = a;
        }
        else if(a>third_max){
            third_max = a;
        }
    }
    return flash>1 ? third_max : first_max;
}
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值