Python deque & set


  • 0
    D
    class PhoneDirectory(object):
    
        def __init__(self, maxNumbers):
            self.maxnum = maxNumbers
            self.available = collections.deque([i for i in range(maxNumbers)])
            self.used = set()
            
        def get(self):
            if not self.available:
                return -1
            res = self.available.popleft()
            self.used.add(res)
            return res
            
        def check(self, number):
            if number < 0 or number >= self.maxnum:
                return False
            return number not in self.used
            
        def release(self, number):
            if number < 0 or number >= self.maxnum or number not in self.used:
                return
            self.used.remove(number)
            self.available.append(number) 
    

Log in to reply
 

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