# 实现 LRU 算法

Map 既可以实现 O(1)查找,又能保存插入的顺序

var LRUCache = function(capacity) {
  this.cache = new Map();
  this.capacity = capacity;
};

LRUCache.prototype.get = function(key) {
  if (this.cache.has(key)) {
    let temp = this.cache.get(key);
    this.cache.delete(key);
    this.cache.set(key, temp);
    return temp;
  }
  return -1;
};

LRUCache.prototype.set = function(key, value) {
  if (this.cache.has(key)) {
    this.cache.delete(key);
  } else if (this.cache.size > this.capacity) {
    this.cache.delete(
      this.cache
        .keys()
        .next()
        .value()
    );
  }
  this.cache.set(key, value);
};
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
最后更新时间: 1/25/2021, 2:35:02 PM