Third Maximum Number

2017-02-12  本文已影响9人  ibyr
Question

Given a non-empty array of integers, return the third maximum number in this array. If it does not exists, return the maximum number. The time complexity must be in O(n).

Note
Extension
Solution
public int thirdMax(int[] nums) {
    Integer max1 = null;
    Integer max2 = null;
    Integer max3 = null;
    for (Integer n : nums) {
        if (n.equals(max1) || n.equals(max2) || n.equals(max3)) {
            continue;
        }
        if (max1 == null || n > max1) {
            max3 = max2;
            max2 = max1;
            max1 = n;
        } else if (max2 == null || n > max2) {
            max3 = max2;
            max2 = n;
        } else if (max3 == null || n > max3) {
            max3 = n;
        }
    }
    return max3 == null ? max1 : max3;
}
// 注:此解法非原创,只供提供学习思路。
上一篇下一篇

猜你喜欢

热点阅读