java using hash set


  • 0
    C
    import java.util.*;
    public class Solution {
    public int thirdMax(int[] nums) {
        Integer res = Integer.MIN_VALUE;
        HashSet<Integer> set = new HashSet<Integer>();
        for(int i=0;i<nums.length;i++)
            set.add(nums[i]);
        
        if(set.size()<3)
        {
            Iterator<Integer> ite = set.iterator();
            while(ite.hasNext())
            {   Integer tmp = ite.next();
                res = res>tmp?res:tmp;
            }
           System.out.println("1res:"+res);
           return res.intValue();
        }
       else
        {
            int i=3;
            while(true)
            {
                Iterator<Integer> ite = set.iterator();
                while(ite.hasNext())
                {   
                    Integer tmp = ite.next();
                    res = res>tmp?res:tmp;
                }
                i--;
                if(i>0)
                {
                    System.out.println("remove:"+res);
                    set.remove(new Integer(res));
                    res = Integer.MIN_VALUE;
                }
                else
                {
                    System.out.println("2res:"+res);
                    return res.intValue();   
                }
            }
        }
        //return 0;
    }
    

    }


Log in to reply
 

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