Java extremely simple solution using TreeSet


  • 0
    S
    public class PhoneDirectory {
        TreeSet<Integer> set = new TreeSet<>();
        /** Initialize your data structure here
            @param maxNumbers - The maximum numbers that can be stored in the phone directory. */
        public PhoneDirectory(int maxNumbers) {
            for(int i=0;i<maxNumbers;i++) set.add(i);
        }
        
        /** Provide a number which is not assigned to anyone.
            @return - Return an available number. Return -1 if none is available. */
        public int get() {
            if(set.size() > 0) {
            int next = set.iterator().next();
            set.remove(next);
            return next;
            }
            return -1;
        }
        
        /** Check if a number is available or not. */
        public boolean check(int number) {
            return set.contains(number);
        }
        
        /** Recycle or release a number. */
        public void release(int number) {
            set.add(number);
        }
    }
    

Log in to reply
 

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