my c++ solution, any suggestion to make it faster??


  • 0
    X

    class PhoneDirectory {
    bool bitset;
    int firstAvailable;
    int capacity;
    vector<int> release_list;
    public:
    /
    * Initialize your data structure here
    @param maxNumbers - The maximum numbers that can be stored in the phone directory. */
    PhoneDirectory(int maxNumbers) {
    bitset = new bool[maxNumbers];
    memset(bitset,true, maxNumbers);
    firstAvailable=0;
    capacity=maxNumbers;
    }

    /** Provide a number which is not assigned to anyone.
        @return - Return an available number. Return -1 if none is available. */
    int get() {
        if(firstAvailable>=capacity && release_list.empty())
        return -1;
        
        if(!release_list.empty()){
            int re=release_list.back();
            bitset[re]=false;
            release_list.pop_back();
            return re;
        }
        
        bitset[firstAvailable]=false;
        ++firstAvailable;
        return firstAvailable-1;
    }
    
    /** Check if a number is available or not. */
    bool check(int number) {
        return bitset[number];
    }
    
    /** Recycle or release a number. */
    void release(int number) {
        if(bitset[number])
        return;
        
        bitset[number]=true;
        release_list.push_back(number);
    }
    

    };


Log in to reply
 

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