[LeetCode 414] Third Maximum Number

来源:互联网 发布:手机版电子杂志软件 编辑:程序博客网 时间:2024/05/22 00:20
  • 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;}};
原创粉丝点击