美文网首页
146. LRU 缓存

146. LRU 缓存

作者: 邦_ | 来源:发表于2022-07-25 15:15 被阅读0次

因为函数 get 和 put 必须以 O(1) 的平均时间复杂度运行。
那只有哈希表能做到了。。
自己想的= =。。用数组存个key。耗时1752 = =。。无语了
因为数组的查找遍历、删除、插入 都是o(n)级别的= =
需要自己实现一个双向链表来优化这个复杂度

class LRUCache {
    var array = Array<Int>()
    var map = Dictionary<Int,Int>()
    var maxLen = 0

    init(_ capacity: Int) {
        
        maxLen = capacity

    }
    
    func get(_ key: Int) -> Int {
        
        if let num = map[key] {
            for (index,tempKey) in array.enumerated() {
                
                if key == tempKey {
                    array.remove(at: index)
                    array.insert(key, at: 0)
                    break
                }
            }
            return num
        }
        return -1

    }
    
    func put(_ key: Int, _ value: Int) {
        
        let len = array.count
        //如果存在的话
        if let _ =  map[key] {
            for (index,tempKey) in array.enumerated() {
                
                if key == tempKey {
                    array.remove(at: index)
                    array.insert(key, at: 0)
                    break
                }
            }
            
        }
        //不存在
        else{
            //达到最大容量了
            if len == maxLen {
                map.removeValue(forKey: array.removeLast())
                array.insert(key, at: 0)
            }else {
                array.insert(key, at: 0)
            }

        }
        map[key] = value
//        print(array)
        
    }
}

双向链表= =。。还是1000多ms

public class LRUNode {
    public var val: Int
    public var key: Int
    public var next: LRUNode?
    public var prev: LRUNode?
    public init() { self.val = 0; self.key = 0;self.next = nil; self.prev = nil; }
    public init(_ key:Int,_ val: Int) { self.val = val;self.key = key; self.next = nil; self.prev = nil; }
}

class LRUCache {
    var map = Dictionary<Int,LRUNode>()
    var maxLen = 0
    var len = 0
    var first  = LRUNode()
    var last  = LRUNode()

    init(_ capacity: Int) {
        
        maxLen = capacity
        first.next = last
        last.prev = first
        
    }
    
    func get(_ key: Int) -> Int {
        
        if let tempNode = map[key] {
            // 删除tempNode
            removeNode(tempNode)
           
            //然后放到前边
            afterFirstNode(tempNode)
            
//            print(tempNode.val)
            
            return tempNode.val
        }
        return -1

    }
    
    func put(_ key: Int, _ value: Int) {
        
        //如果存在的话
        if let tempNode =  map[key] {
    
            tempNode.val = value
            map[key] = tempNode

            // 删除tempNode
            removeNode(tempNode)
            //然后放到前边
            afterFirstNode(tempNode)
            
            
        }
        //不存在
        else{        
            //达到最大容量了
            if len == maxLen {
                
                // 删除最后一个节点
//                print(last.prev!.val)
                let lastNode = last.prev!
                removeNode(lastNode)
                map.removeValue(forKey: lastNode.key)
                let tempNode = LRUNode(key,value)
                map[key] = tempNode
                afterFirstNode(tempNode)
                
                
            }else {
                
                
                let tempNode = LRUNode(key,value)
                map[key] = tempNode
                len += 1
                afterFirstNode(tempNode)
                
                
            }

        }
        
        
        
    }
    
    
    func removeNode(_ tempNode:LRUNode){
        
        //前一个节点
        let prevNode = tempNode.prev
        //后一个节点
        let lastNode = tempNode.next
        prevNode?.next = lastNode
        lastNode?.prev = prevNode
        
        
    }
    
    func afterFirstNode(_ tempNode:LRUNode){
        
        let afterNode = first.next!
        tempNode.prev = first
        first.next = tempNode
        tempNode.next = afterNode
        afterNode.prev = tempNode

    }
       
    
}


继续优化下= =。。



public class LRUNode {
    public var val: Int
    public var key: Int
    public var next: LRUNode?
    public var prev: LRUNode?
    public init() { self.val = 0; self.key = 0;self.next = nil; self.prev = nil; }
    public init(_ key:Int,_ val: Int) { self.val = val;self.key = key; self.next = nil; self.prev = nil; }
}

class LRUCache {
    var map = Dictionary<Int,LRUNode>()
    var maxLen = 0
    var len = 0
    var first  = LRUNode()
    var last  = LRUNode()

    init(_ capacity: Int) {
        
        maxLen = capacity
        first.next = last
        last.prev = first
        
    }
    
    func get(_ key: Int) -> Int {
        
        if let tempNode = map[key] {
            // 删除tempNode
            removeNode(tempNode)
           
            //然后放到前边
            afterFirstNode(tempNode)
            
//            print(tempNode.val)
            
            return tempNode.val
        }
        return -1

    }
    
    func put(_ key: Int, _ value: Int) {
        
        //如果存在的话
        if let tempNode =  map[key] {
    
            tempNode.val = value
            // 删除tempNode
            removeNode(tempNode)
            //然后放到前边
            afterFirstNode(tempNode)
            
            
        }
        //不存在
        else{        
            //达到最大容量了
            if len == maxLen {
                
                // 删除最后一个节点
//                print(last.prev!.val)
                let lastNode = last.prev!
                removeNode(lastNode)
                map.removeValue(forKey: lastNode.key)
                let tempNode = LRUNode(key,value)
                map[key] = tempNode
                afterFirstNode(tempNode)
                
                
            }else {
                
                
                let tempNode = LRUNode(key,value)
                map[key] = tempNode
                len += 1
                afterFirstNode(tempNode)
                
                
            }

        }
        
        
        
    }
    
    
    func removeNode(_ tempNode:LRUNode){
        
      tempNode.prev?.next = tempNode.next
      tempNode.next?.prev = tempNode.prev
        
        
    }
    
    func afterFirstNode(_ tempNode:LRUNode){
        
       let afterNode = first.next!
        tempNode.prev = first
        first.next = tempNode
        tempNode.next = afterNode
        afterNode.prev = tempNode

    }

    
}






相关文章

  • LeetCode 146-150

    146. LRU缓存机制[https://leetcode-cn.com/problems/lru-cache/]...

  • LRU(leetcode.146)

    146. LRU 缓存机制[https://leetcode-cn.com/problems/lru-cache/...

  • 146. LRU 缓存

    146. LRU 缓存[https://leetcode.cn/problems/lru-cache/] 请你设计...

  • 146. LRU 缓存

    题目地址(146. LRU 缓存) https://leetcode.cn/problems/lru-cache/...

  • LeetCode 146. LRU缓存机制(LRU Cache)

    146. LRU缓存机制 Python3 实现 LRU(最近最少使用) 缓存机制 更多可参见:https://en...

  • LRU缓存

    146. LRU缓存 设计和实现一个LRU(最近最少使用)的缓存机制。它可以支持以下操作: get 和 put 。...

  • LeetCode146 动手实现LRU算法

    146. LRU缓存机制 运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持...

  • LRU缓存机制的实现

    leetcode 146. LRU(Least Recently Used)是一种缓存替换策略。现实生活中,缓存的...

  • 实现LRU缓存算法

    本文基于LeetCode第146. LRU 缓存机制[https://leetcode-cn.com/proble...

  • 算法第4天:LRU缓存机制

    leetcode 146. LRU缓存机制 middle 运用你所掌握的数据结构,设计和实现一个 LRU (最...

网友评论

      本文标题:146. LRU 缓存

      本文链接:https://www.haomeiwen.com/subject/gummirtx.html