[Wrong Answer] But I got the right expected result under my computer


  • 0
    H

    Reported by OJ system.

    Input:	2,[set(2,1),set(1,1),get(2),set(4,1),get(1),get(2)]
    Output:	[1,1,-1]
    Expected:	[1,-1,1]
    

    But I have the right expected result [1,-1,1] when the same code is executed under my computer.

    PS: All my code is the following block

    class LRUCache:
        limit = 0
        num = 0
        bucket = dict()
        keys = list()
        def __init__(self,capacity):
            self.limit = capacity
    
        def set(self,key,value):
            if(self.num >= self.limit):
                # remove the least one element.
                remove_key = self.keys.pop(0)
                if(self.bucket.has_key(remove_key)):
                    self.bucket.pop(remove_key)
            else:
                self.num = self.num + 1
            self.keys.append(key)
            self.bucket[key] = value
    
        def get(self,key):
            if(not self.bucket.has_key(key)):
                return -1
            #update the Most recently used obj
            self.keys.pop(self.keys.index(key))
            self.keys.append(key)
            return self.bucket[key]

  • 0
    H

    #add the same testing sequence in Python code.

    lru=LRUCache(2)

    lru.set(2,1)

    lru.set(1,1)

    print lru.get(2)

    lru.set(4,1)

    print lru.get(1)

    print lru.get(2)


  • 0
    D

    I met the same error , do you know the reason? thx


Log in to reply
 

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