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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| #include <iostream> #include <vector> #include <unordered_map> #include <list> using namespace std;
class LRUCache { public: LRUCache(int capacity) { this->capacity = capacity; head = new Node(), tail = new Node(); head->next = tail, tail->prev = head; }
int get(int key) { if (!cache.count(key)) { return -1; } Node* p = cache[key]; removeNode(p); addNode(p); return p->val; } void put(int key, int value) { if (!cache.count(key)) { if (cache.size() == capacity) { Node* p = tail->prev; removeNode(p); cache.erase(p->key); delete p; } Node* p = new Node(key, value); addNode(p); cache[key] = p; } else { Node* p = cache[key]; p->val = value; removeNode(p); addNode(p); } }
private: struct Node { int key, val; Node *prev, *next; Node() : key(-1), val(-1), prev(nullptr), next(nullptr) {} Node(int k, int v) : key(k), val(v), prev(nullptr), next(nullptr) {} }; Node *head, *tail; unordered_map<int, Node*> cache; int capacity;
void addNode(Node* p) { p->prev = head; p->next = head->next; head->next->prev = p; head->next = p; }
void removeNode(Node* p) { p->next->prev = p->prev; p->prev->next = p->next; } };
void printArray(const vector<string>& nums) { cout << "["; for (size_t i = 0; i < nums.size(); i++) { cout << nums[i]; if (i != nums.size() - 1) cout << ","; } cout << "]"; }
void printArray(const vector<int>& nums) { cout << "["; for (size_t i = 0; i < nums.size(); i++) { if (nums[i] == NULL) { cout << "null"; } else { cout << nums[i]; } if (i != nums.size() - 1) cout << ","; } cout << "]"; }
void printArray(const vector<vector<int>>& nums) { cout << "["; for (size_t i = 0; i < nums.size(); i++) { cout << "["; for (size_t j = 0; j < nums[i].size(); j++) { cout << nums[i][j]; if (j != nums[i].size() - 1) cout << ","; } cout << "]"; if (i != nums.size() - 1) cout << ","; } cout << "]"; }
int main() { LRUCache* obj = new LRUCache(2); vector<string> operations = {"LRUCache","put","put","get","put","get","put","get","get","get"}; vector<vector<int>> params = {{2},{1,1},{2,2},{1},{3,3},{2},{4,4},{1},{3},{4}}; vector<int> output;
for (int i = 0; i < operations.size(); i++) { string op = operations[i]; if (op == "LRUCache") { output.push_back(NULL); } else if (op == "put") { obj->put(params[i][0], params[i][1]); output.push_back(NULL); } else if (op == "get") { int res = obj->get(params[i][0]); output.push_back(res); } }
cout << "Input: " << endl; printArray(operations); cout << endl; printArray(params); cout << endl; cout << "Output: " << endl; printArray(output); cout << endl;
delete obj;
return 0; }
|