[JAVA] Clean Code [1 Solutions]


  • 0
    R

    [JAVA] Clean Code [1 Solutions]


    Full Solutions

    Solution 1

    public class Solution {
        public int thirdMax(int[] nums) {
            Integer first = null;
            Integer second = null;
            Integer third = null;
            for (int n : nums) {
                if (first == null || n > first) {
                    third = second;
                    second = first;
                    first = n;
                } else if (second == null || n > second) {
                    if (n == first) continue;
                    third = second;
                    second = n;
                } else if (third == null || n > third) {
                    if (n == second) continue;
                    third = n;
                }
            }
            return third == null ? first : third;
        }
    }
    


Log in to reply
 

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