Someone help me out please! why my code couldn't pass?


  • 0
    X
    class LRUCache:
    # @param capacity, an integer
    def __init__(self, capacity):
        self.capacity = capacity      
        self.cache_dict = {}
        self.key_list = []
        
    def __str__(self):
        return str(self.cache_dict) 
    
    # @return an integer
    def get(self, key):
        self.key = key
        if self.key in self.cache_dict:
            self.key_list.remove(self.key)
            self.key_list.append(self.key)    #move the expected value to the tail in the list
            return self.cache_dict[self.key_list[-1]]
        else:
            return -1
    
    # @param key, an integer
    # @param value, an integer
    # @return nothing
    def set(self, key, value):
        self.key = key
        self.value = value
        if len(self.key_list) == self.capacity:
            self.cache_dict.pop(self.key_list[0])   #pop out the first(least recent used) one 
            tem = self.key_list.pop(0)
            print 'here deleted ' + str(tem)
        self.key_list.append(self.key)
        self.cache_dict[self.key] = self.value
    

    Last executed input: 2,[set(2,1),set(1,1),set(2,3),set(4,1),get(1),get(2)]

    Can't figure out why my code could not pass.... Someone help me out?
    BTW, what is the expected output format? Or no need to worry about that?


  • 1
    S

    You do not need to concern about output, LeetCode will cover that. However, you need to pay attention do not print anything in your code, it will influence the judge result.


Log in to reply
 

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