What is wrong with my code?


  • 0
    L

    ...
    public class PhoneDirectory {
    private LinkedList<Integer> mylist;
    private HashSet<Integer> used;
    private int num=0;

    /** Initialize your data structure here
        @param maxNumbers - The maximum numbers that can be stored in the phone directory. */
    public PhoneDirectory(int maxNumbers) {
         mylist = new LinkedList<Integer>();
         used = new HashSet<Integer>();
         num=maxNumbers;
         for(int i=0; i<maxNumbers; i++){
             mylist.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(mylist.size()==0){
            return -1;
        }
        Integer temp = mylist.remove();
        used.add(temp);
        return temp;
    }
    
    /** Check if a number is available or not. */
    public boolean check(int number) {
        Iterator<Integer> i = mylist.iterator();
        while(i.hasNext()){
            if(i.next()==number){
                return true;
            }
        }
        return false;
    }
    
    /** Recycle or release a number. */
    public void release(int number) {
        if(used.contains(number)){
            used.remove(number);
            mylist.add(number);
        }
    }
    

    }

    ...


Log in to reply
 

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