Java Easy Solution


  • 0
    W
    public class Solution {
        public int thirdMax(int[] nums) {
            long max = Long.MIN_VALUE, secMax = Long.MIN_VALUE, trdMax = Long.MIN_VALUE;
            for (int n : nums) {
                if (n == max || n == secMax || n == trdMax) continue;
                if (n > max) {
                    trdMax = secMax;
                    secMax = max;
                    max = n;
                } else if (n > secMax) {
                    trdMax = secMax;
                    secMax = n;
                } else if (n > trdMax) {
                    trdMax = n;
                }
            }
            return trdMax < Integer.MIN_VALUE ? (int) max : (int) trdMax;
        }
    }

Log in to reply
 

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