Java noob Easy understand solution


  • 0
    O
    public class Solution {
        static long[] numbers;
        public int thirdMax(int[] nums) {
            int n = nums.length;
            numbers = new long[n];
            for(int i = 0; i < n; i++){
                numbers[i] = nums[i];
            }
            long[] res = new long[3];
            Arrays.fill(res,Long.MIN_VALUE);
            for(int i = 0; i < 3; i++){
                res[i] = findMax(numbers);
            }
            
            long result = res[2] != Long.MIN_VALUE? res[2] : res[0];
            return (int)result;
        }
        
        public long findMax(long[] nums) {
            long max = Long.MIN_VALUE;
            for(int i = 0; i < nums.length; i++){
                if(nums[i] > max){
                    max = nums[i];
                } 
            }
            for(int i = 0; i < nums.length; i++){
                if(nums[i] == max) nums[i] = Long.MIN_VALUE;
            }
            return max;
        }
    }
    

Log in to reply
 

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