My simple AC Java with one set.


  • 1

    Using a set is obvious. I think the main challenge of this question is to figure out a way to pop an element from the set. I tried iterator, which is ETL. Eventually, I found an even simpler way to do it.

    public class PhoneDirectory {
        Set<Integer> phones; 
    
        public PhoneDirectory(int maxNumbers) {
            phones = new HashSet<>(); 
            for(int i=0; i<maxNumbers; i++) phones.add(i);    
        }    
    
        public int get() {
            if(phones.size()<=0) return -1; 
            else{
                int temp = 0; 
                for(int nn : phones){
                    temp = nn; 
                    break; 
                }
                phones.remove(temp);
                return temp;
            }
        }
        
        public boolean check(int number) {
            return phones.contains(number); 
        }
        
        public void release(int number) {
            phones.add(number); 
        }
    }
    

Log in to reply
 

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