@@ -0,0 +1,1333 @@
+package ThirdMaximumNumber;%0A%0Aimport java.util.Arrays;%0A%0A/**%0A * Given a non-empty array of integers, return the third maximum number in this array. If it does%0A * not exist, return the maximum number. The time complexity must be in O(n).%0A * %3Cpre%3E%0A * Example 1:%0A * Input: %5B3, 2, 1%5D%0A *%0A * Output: 1%0A *%0A * Explanation: The third maximum is 1.%0A * Example 2:%0A * Input: %5B1, 2%5D%0A *%0A * Output: 2%0A *%0A * Explanation: The third maximum does not exist, so the maximum (2) is returned instead.%0A * Example 3:%0A * Input: %5B2, 2, 3, 1%5D%0A *%0A * Output: 1%0A *%0A *%0A * Explanation: Note that the third maximum here means the third maximum distinct number.%0A *%0A * %3C/pre%3E%0A * Both numbers with value 2 are both considered as second maximum.%0A */%0Apublic class Solution %7B%0A    public int thirdMax(int%5B%5D nums) %7B%0A        int%5B%5D maxs = new int%5B3%5D;%0A        int n = 0;%0A%0A        for (int num : nums) %7B%0A            boolean in = false;%0A            for (int i = 0; i %3C n; i++) %7B%0A                if (maxs%5Bi%5D == num) %7B%0A                    in = true;%0A                    break;%0A                %7D%0A            %7D%0A            if (in) continue;%0A%0A            if (n %3C 3) %7B%0A                maxs%5Bn++%5D = num;%0A            %7D else %7B%0A                maxs%5B0%5D = Math.max(maxs%5B0%5D, num);%0A            %7D%0A            Arrays.sort(maxs, 0, 3);%0A        %7D%0A        return n %3C 3 ? maxs%5Bn - 1%5D : maxs%5B0%5D;%0A    %7D%0A%7D

414. Third Maximum Number