Ruby Hash-only solution. Leverage Hash characters of key ordering


  • 0
    S
    class LRUCache
        def initialize(capacity)
            @capacity = capacity
            @storage  = {}
        end
    
        def get(key)
            @storage[key] ? @storage[key] = @storage.delete(key) : -1
        end
    
        def set(key, value)
            if @storage[key]
                @storage.delete(key)
            else
                @storage.shift if @storage.length >= @capacity
            end
            @storage[key] = value
            nil
        end
    end
    

Log in to reply
 

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