JAVA Concise O(n) + O(1) Solution


  • 0
    public class Solution {
        public int thirdMax(int[] nums) {
            int a = Integer.MIN_VALUE;
            int b = Integer.MIN_VALUE;
            int c = Integer.MIN_VALUE;
            for (int num : nums) {
                if (num > a) {
                    c = b;
                    b = a;
                    a = num;
                }
                else if (num > b && num != a) {
                    c = b;
                    b = num;
                }
                else if (num > c && num != a && num != b) {
                    c = num;
                }
            }
            return nums.length < 3 ? a : c; 
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.