[LeetCode] Third Maximum Number 第三大的數


 

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.

 

這道題讓我們求數組中第三大的數,如果不存在的話那么就返回最大的數,題目中說明了這里的第三大不能和第二大相同,必須是嚴格的小於,而並非小於等於。這道題並不是很難,如果知道怎么求第二大的數,那么求第三大的數的思路都是一樣的。那么我們用三個變量first, second, third來分別保存第一大,第二大,和第三大的數,然后我們遍歷數組,如果遍歷到的數字大於當前第一大的數first,那么三個變量各自錯位賦值,如果當前數字大於second,小於first,那么就更新second和third,如果當前數字大於third,小於second,那就只更新third,注意這里有個坑,就是初始化要用長整型long的最小值,否則當數組中有INT_MIN存在時,程序就不知道該返回INT_MIN還是最大值first了,參見代碼如下:

 

解法一:

class Solution {
public:
    int thirdMax(vector<int>& nums) {
        long first = LONG_MIN, second = LONG_MIN, third = LONG_MIN;
        for (int num : nums) {
            if (num > first) {
                third = second;
                second = first;
                first = num;
            } else if (num > second && num < first) {
                third = second;
                second = num;
            } else if (num > third && num < second) {
                third = num;
            }
        }
        return (third == LONG_MIN || third == second) ? first : third;
    }
};

 

下面這種方法的時間復雜度是O(nlgn),不符合題目要求,純粹是拓寬下思路哈,利用了set的自動排序和自動去重復項的特性,很好的解決了問題,對於遍歷到的數字,加入set中,重復項就自動去掉了,如果此時set大小大於3個了,那么我們把set的第一個元素去掉,也就是將第四大的數字去掉,那么就可以看出set始終維護的是最大的三個不同的數字,最后遍歷結束后,我們看set的大小是否為3,是的話就返回首元素,不是的話就返回尾元素,參見代碼如下:

 

解法二:

class Solution {
public:
    int thirdMax(vector<int>& nums) {
        set<int> s;
        for (int num : nums) {
            s.insert(num);
            if (s.size() > 3) {
                s.erase(s.begin());
            }
        }
        return s.size() == 3 ? *s.begin() : *s.rbegin();
    }
};

 

參考資料:

https://discuss.leetcode.com/topic/63903/short-easy-c-using-set

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM