java simple code using minvalue


  • 0
    C
    public class Solution {
        public int thirdMax(int[] nums) {
            int res = Integer.MIN_VALUE;
            int min = Integer.MAX_VALUE;
            for(int i : nums){
                res = Math.max(res,i);
                min = Math.min(min,i);
            }
            int one = min;
            int two = min;
            int three = min;
            
            for(int i : nums){
                if(i > one){
                    three = two;
                    two = one;
                    one = i;
                }else if(i < one && i > two){
                    three = two;
                    two = i;
                }else if(i < two && i > three){
                    three = i;
                }
            }
            if(one == two || two == three) return res;
            else return three;
        }
    }

Log in to reply
 

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