1. <?php
    2. /**
    3. * https://leetcode-cn.com/problems/lru-cache/
    4. *
    5. * 146. LRU缓存机制
    6. * 思路:哈希表 + 双向链表
    7. *
    8. * Class ListNode
    9. */
    10. class ListNodeNew{
    11. public $data = null;
    12. public $key = null;
    13. public $prev = null;
    14. public $next = null;
    15. public function __construct($key,$data)
    16. {
    17. $this->data = $data;
    18. $this->key = $key;
    19. }
    20. }
    21. class LRUCacheNew {
    22. private $head = null;
    23. private $tail = null;
    24. private $capacity = null;
    25. private $hashmap = null;
    26. /**
    27. * @param Integer $capacity
    28. */
    29. function __construct($capacity) {
    30. // hash map结果
    31. $this->hashmap = array();
    32. $this->capacity = $capacity;
    33. // 构建虚拟的双向链表
    34. $this->head = new ListNodeNew(null,null);
    35. $this->tail = new ListNodeNew(null,null);
    36. $this->head->next = $this->tail;
    37. $this->tail->next = $this->head;
    38. }
    39. /**
    40. * @param Integer $key
    41. * @return Integer
    42. */
    43. function get($key) {
    44. if (!$this->hashmap[$key]){
    45. return -1;
    46. }
    47. $node = $this->hashmap[$key];
    48. if (count($this->hashmap) == 1) {
    49. return $node->data;
    50. }
    51. $this->delete($node);
    52. $this->attach($this->head,$node);
    53. return $node->data;
    54. }
    55. /**
    56. * 将节点前面的next指向节点后面节点
    57. * 将节点后面的prev指向节点前面的节点
    58. * @param $node
    59. */
    60. public function delete($node){
    61. $node->prev->next = $node->next;
    62. $node->next->prev = $node->prev;
    63. }
    64. /**
    65. * 头部增加节点
    66. * @param $head
    67. * @param $node
    68. */
    69. public function attach($head,$node){
    70. $node->prev = $head;
    71. $node->next = $head->next;
    72. $node->prev->next = $node;
    73. $node->next->prev = $node;
    74. }
    75. /**
    76. * 插入数据
    77. * @param Integer $key
    78. * @param Integer $value
    79. * @return NULL
    80. */
    81. function put($key, $value) {
    82. if ($this->capacity <= 0){
    83. return false;
    84. }
    85. if (empty($this->hashmap[$key])){
    86. // 刚好到了容量
    87. if (count($this->hashmap) == $this->capacity) {
    88. // 删除尾部元素
    89. $nodeToRemove = $this->tail->prev;
    90. $this->delete($nodeToRemove);
    91. unset($this->hashmap[$nodeToRemove->key]);
    92. }
    93. // 插入新元素
    94. $newNode = new ListNodeNew($key,$value);
    95. $this->attach($this->head,$newNode);
    96. $this->hashmap[$key] = $newNode;
    97. }else{
    98. $newNode = $this->hashmap[$key];
    99. // 删除后面的节点,插入到前面
    100. $this->delete($newNode);
    101. $this->attach($this->head,$newNode);
    102. $newNode->data = $value;
    103. }
    104. }
    105. }
    106. $cache = new LRUCacheNew(1);
    107. $cache->put(2,1);
    108. print_r($cache->get(2));
    109. die();
    110. //---------output---------------------------------
    111. //LRUCache cache = new LRUCache( 2 /* 缓存容量 */ );
    112. //
    113. //cache.put(1, 1);
    114. //cache.put(2, 2);
    115. //cache.get(1); // 返回 1
    116. //cache.put(3, 3); // 该操作会使得密钥 2 作废
    117. //cache.get(2); // 返回 -1 (未找到)
    118. //cache.put(4, 4); // 该操作会使得密钥 1 作废
    119. //cache.get(1); // 返回 -1 (未找到)
    120. //cache.get(3); // 返回 3
    121. //cache.get(4); // 返回 4
    //哈希表 + 双向链表
    
    type LRUCache struct {
        size int
        capacity int
        cache map[int]*DLinkedNode
        head, tail *DLinkedNode
    }
    
    type DLinkedNode struct {
        key, value int
        prev, next *DLinkedNode
    }
    
    func initDLinkedNode(key, value int) *DLinkedNode {
        return &DLinkedNode{
            key: key,
            value: value,
        }
    }
    
    func Constructor(capacity int) LRUCache {
        l := LRUCache{
            cache: map[int]*DLinkedNode{},
            head: initDLinkedNode(0, 0),
            tail: initDLinkedNode(0, 0),
            capacity: capacity,
        }
        l.head.next = l.tail
        l.tail.prev = l.head
        return l
    }
    
    func (this *LRUCache) Get(key int) int {
        if _, ok := this.cache[key]; !ok {
            return -1
        }
        node := this.cache[key]
        this.moveToHead(node)
        return node.value
    }
    
    
    func (this *LRUCache) Put(key int, value int)  {
        if _, ok := this.cache[key]; !ok {
            node := initDLinkedNode(key, value)
            this.cache[key] = node
            this.addToHead(node)
            this.size++
            if this.size > this.capacity {
                removed := this.removeTail()
                delete(this.cache, removed.key)
                this.size--
            }
        } else {
            node := this.cache[key]
            node.value = value
            this.moveToHead(node)
        }
    }
    
    func (this *LRUCache) addToHead(node *DLinkedNode) {
        node.prev = this.head
        node.next = this.head.next
        this.head.next.prev = node
        this.head.next = node
    }
    
    func (this *LRUCache) removeNode(node *DLinkedNode) {
        node.prev.next = node.next
        node.next.prev = node.prev
    }
    
    func (this *LRUCache) moveToHead(node *DLinkedNode) {
        this.removeNode(node)
        this.addToHead(node)
    }
    
    func (this *LRUCache) removeTail() *DLinkedNode {
        node := this.tail.prev
        this.removeNode(node)
        return node
    }
    
    作者:LeetCode-Solution
    链接:https://leetcode-cn.com/problems/lru-cache/solution/lruhuan-cun-ji-zhi-by-leetcode-solution/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。