11/18 passed Python solution, easy to understand


  • 0
    W

    class PhoneDirectory(object):

    def __init__(self, maxNumbers):
        """
        Initialize your data structure here
        @param maxNumbers - The maximum numbers that can be stored in the phone directory.
        :type maxNumbers: int
        """
        self.used = []
        self.maxnumber = maxNumbers
        self.curNum = 0
        
    
    def get(self):
        """
        Provide a number which is not assigned to anyone.
        @return - Return an available number. Return -1 if none is available.
        :rtype: int
        """
        print "get called"
        if len(self.used) > 0:
            res = self.used[len(self.used) - 1] + 1
            if res >=0 and res < (self.maxnumber - 1):
                self.used.append(res)
                
                print "return res = ", res
                return res
        
        for i in range(self.maxnumber):
            #print "now check ", i
            
            if i not in self.used:
                #print "found non-used number: ", i
                self.used.append(i)
                return i
    			
    def check(self, number):
        """
        Check if a number is available or not.
        :type number: int
        :rtype: bool
        """
        #print "1 check called, number = ", number
        #if  number:
            #return False
            
        #print "2 check called, number = ", number
        
        if (number-1) > self.maxnumber:
            return False
            
        elif number in self.used:
            return False
        else:
            return True
        
    
    def release(self, number):
        """
        Recycle or release a number.
        :type number: int
        :rtype: void
        """
        #print "release number ", number
        
        if number in self.used:
            self.used.remove(number)

Log in to reply
 

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