本文最后更新于:2022年7月20日 下午
java写法,c++的代码一直调不对,不知道怎么了。思路就是自定义一个双向链表的类DLinkedNode,然后一个map<int,DLinkedNode>存key所对应的链表节点。 tips:
使用 2个虚拟头节点和虚拟尾节点会更加方便处理链表节点的删除和插入操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 class LRUCache { class DLinkedNode { int key; int value; DLinkedNode prev; DLinkedNode next; public DLinkedNode () {} public DLinkedNode (int k, int v) {key = k; value = v;} } private Map<Integer, DLinkedNode> cache = new HashMap <Integer, DLinkedNode>(); private int size; private int capacity; private DLinkedNode head, tail; public LRUCache (int capacity) { this .size = 0 ; this .capacity = capacity; head = new DLinkedNode (); tail = new DLinkedNode (); head.next = tail; tail.prev = head; } void removeNode (DLinkedNode node) { node.next.prev = node.prev; node.prev.next = node.next; } void addToHead (DLinkedNode node) { node.prev = head; node.next = head.next; head.next.prev = node; head.next = node; } void moveToHead (DLinkedNode node) { removeNode(node); addToHead(node); } public int get (int key) { DLinkedNode node = cache.get(key); if (node == null ) { return -1 ; } moveToHead(node); return node.value; } DLinkedNode removeTail () { DLinkedNode node = tail.prev; removeNode(node); return node; } public void put (int key, int value) { DLinkedNode node = cache.get(key); if (node == null ) { DLinkedNode newNode = new DLinkedNode (key, value); cache.put(key, newNode); addToHead(newNode); ++size; if (size > capacity) { DLinkedNode removed = removeTail(); cache.remove(removed.key); --size; } } else { node.value = value; moveToHead(node); } } }
c++写法 ? size前有int 输出结果就和预期的不一致
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 struct DLinkedNode { int key; int value; DLinkedNode* prev; DLinkedNode* next; DLinkedNode () : key (0 ), value (0 ), prev (NULL ), next (NULL ) {} DLinkedNode (int k, int v) : key (k), value (v), prev (NULL ), next (NULL ) {} }; class LRUCache {private : unordered_map<int , DLinkedNode*> cacheLinked; DLinkedNode* dummy_head; DLinkedNode* dummy_tail; int size; int capacity;public : LRUCache (int _capacity) { size = 0 ; capacity = _capacity; dummy_head = new DLinkedNode (); dummy_tail = new DLinkedNode (); dummy_head->next = dummy_tail; dummy_tail->prev = dummy_head; } void removeNode (DLinkedNode* node) { node->prev->next = node->next; node->next->prev = node->prev; } void addToHead (DLinkedNode* node) { node->prev = dummy_head; node->next = dummy_head->next; dummy_head->next->prev = node; dummy_head->next = node; } void moveToHead (DLinkedNode* node) { removeNode (node); addToHead (node); } int get (int key) { if (!cacheLinked.count (key)) return -1 ; DLinkedNode* node = cacheLinked[key]; moveToHead (node); return node->value; } DLinkedNode* removeTail () { DLinkedNode* node = dummy_tail->prev; removeNode (node); return node; } void put (int key, int value) { if (!cacheLinked.count (key)) { DLinkedNode* node = new DLinkedNode (key, value); cacheLinked[key] = node; addToHead (node); ++size; if (size > capacity) { DLinkedNode* removed = removeTail (); cacheLinked.erase (removed->key); delete removed; --size; } } else { DLinkedNode* node = cacheLinked[key]; node->value = value; moveToHead (node); } } };