BiruLyu
7/3/2017 - 4:59 AM

414. Third Maximum Number(1st).java

public class Solution {
    public int thirdMax(int[] nums) {
        //[1,2,-2147483648]
        if (nums == null || nums.length < 1) return Integer.MIN_VALUE;
        Integer first = null;
        Integer second = null;
        Integer third = null;
        for (Integer num : nums) {
            if (num.equals(first) || num.equals(second) || num.equals(third)) continue;
            if (first == null || num > first) {
                third = second;
                second = first;
                first = num;
            } else if (second == null || num > second ) {
                third = second;
                second = num;
            } else if (third == null || num > third ) {
                third = num;
            }
        }
        return third == null ? first : third; 
    }
}
public class Solution {
    public int thirdMax(int[] nums) {
          if(nums == null || nums.length == 0){
              return Integer.MIN_VALUE;
          }
          long first = Long.MIN_VALUE, second = Long.MIN_VALUE, third = Long.MIN_VALUE;
        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_VALUE || third == second) ? (int)first : (int)third;
    }
}