Not very good but easy and understandable acceptable solution


  • 0
    M
        public int thirdMax(int[] nums) {
            SortedSet<Integer> set = new TreeSet<Integer>(Collections.reverseOrder()); 
    	        if(nums.length==0) return 0;
    	        for(int i=0;i<nums.length;i++)
    	        	set.add(nums[i]);
    	        if(set.size()==1)
            		return getElement(set, 1);
    	        else if(set.size()==2)
            		return getElement(set, 1);
    	        else return getElement(set,3);
        }
        public static int getElement(SortedSet<Integer> set,int position)
        {
    		int i=1;
            for(int n:set)
            {
            	if(position == i)
            	    return n;
            	i++;
            }
        	return 0;
        }
    }

Log in to reply
 

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