-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path146. LRUCache.js
96 lines (86 loc) · 1.93 KB
/
146. LRUCache.js
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
/**
* Note:
* 1. Double Linked List: ListNode(key, val, prev, next)
* 2. LRUCache(head, tail, size, capacity, map, attachToHead, moveToHead, removeLast)
* 3. Check empty node: typeof node !== 'undefined'
*/
'use strict';
function ListNode(key, val) {
this.key = key;
this.val = val;
this.prev = null;
this.next = null;
}
/**
* @constructor
*/
var LRUCache = function(capacity) {
this.head = new ListNode(-1, -1);
this.tail = new ListNode(-1, -1);
this.head.next = this.tail;
this.tail.prev = this.head;
this.size = 0;
this.capacity = capacity;
this.map = new Map();
this.attachToHead = function (node) {
node.next = this.head.next;
node.next.prev = node;
this.head.next = node;
node.prev = this.head;
}
this.moveToHead = function (node) {
node.prev.next = node.next;
node.next.prev = node.prev;
this.attachToHead(node);
}
this.removeLast = function () {
let last = this.tail.prev;
last.prev.next = this.tail;
this.tail.prev = last.prev;
this.map.delete(last.key);
}
};
/**
* @param {number} key
* @returns {number}
*/
LRUCache.prototype.get = function(key) {
let node = this.map.get(key);
if (typeof node !== 'undefined') {
this.moveToHead(node);
return node.val;
} else {
return -1;
}
};
/**
* @param {number} key
* @param {number} value
* @returns {void}
*/
LRUCache.prototype.set = function(key, value) {
let node = this.map.get(key);
if (typeof node === 'undefined') {
node = new ListNode(key, value);
this.attachToHead(node);
this.size++;
} else {
node.val = value;
this.moveToHead(node);
}
if (this.size > this.capacity) {
this.removeLast();
this.size--;
}
this.map.set(key, node);
};
let cache = new LRUCache(10);
cache.set(10, 13);
cache.set(3, 17);
cache.set(6, 11);
cache.set(10, 5);
cache.set(9, 10);
console.log(cache.get(13));
cache.set(2, 19);
console.log(cache.get(2));
console.log(cache.get(13));