java ,TreeSet, simple


  • 0

    public int thirdMax(int[] nums) {

        TreeSet<Integer> treeSet =new TreeSet<Integer>();
    
        for (Integer n : nums) {
            treeSet.add(n);
        }
        Iterator<Integer> it = treeSet.descendingIterator();
    
        int j=0;
        int result=0;
    
        while (it.hasNext() && j<3){
             result=it.next();
            j++;
        }
    
        return j==3?result:treeSet.last();
    }

Log in to reply
 

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