|
2 | 2 | * @param {number} capacity |
3 | 3 | */ |
4 | 4 |
|
5 | | -var Node = function(key, val) { |
6 | | - this.prev = null; |
7 | | - this.next = null; |
8 | | - this.val = val; |
9 | | - this.key = key; |
10 | | -}; |
| 5 | +class LRUCache { |
| 6 | + capacity: number; |
| 7 | + count: number; |
| 8 | + head: ListNode; |
| 9 | + tail: ListNode; |
| 10 | + hash: { [key: string]: any }; |
11 | 11 |
|
12 | | -var LRUCache = function(capacity) { |
13 | | - this.capacity = capacity; |
14 | | - this.count = 0; |
15 | | - this.head = new Node(-1, -1); |
16 | | - this.tail = new Node(-1, -1); |
17 | | - this.head.next = this.tail; |
18 | | - this.tail.prev = this.head; |
19 | | - this.hash = {}; |
20 | | -}; |
21 | | - |
22 | | -/** |
23 | | - * @param {number} key |
24 | | - * @return {number} |
25 | | - */ |
26 | | -LRUCache.prototype.get = function(key) { |
27 | | - if (this.hash[key]) { |
28 | | - const currentNode = this.hash[key]; |
29 | | - const tempPrev = currentNode.prev; |
30 | | - const tempNext = currentNode.next; |
31 | | - const tempPrevTail = this.tail.prev; |
32 | | - const tempPrevNext = this.tail.next; |
33 | | - tempPrev.next = tempNext; |
34 | | - tempNext.prev = tempPrev; |
35 | | - this.tail.prev.next = currentNode; |
36 | | - currentNode.prev = tempPrevTail; |
37 | | - currentNode.next = this.tail; |
38 | | - this.tail.prev = currentNode; |
39 | | - return this.hash[key].value; |
| 12 | + constructor(capacity: number) { |
| 13 | + this.capacity = capacity; |
| 14 | + this.count = 0; |
| 15 | + this.head = new ListNode({ key: -1, value: -1 }); |
| 16 | + this.tail = new ListNode({ key: -1, value: -1 }); |
| 17 | + this.head.next = this.tail; |
| 18 | + this.tail.prev = this.head; |
| 19 | + this.hash = {}; |
40 | 20 | } |
41 | 21 |
|
42 | | - return -1; |
43 | | -}; |
| 22 | + get(key: number): number { |
| 23 | + if (this.hash[key]) { |
| 24 | + const currentNode = this.hash[key]; |
| 25 | + const tempPrev = currentNode.prev; |
| 26 | + const tempNext = currentNode.next; |
| 27 | + const tempPrevTail = this.tail.prev; |
| 28 | + //const tempPrevNext = this.tail.next; |
| 29 | + tempPrev.next = tempNext; |
| 30 | + tempNext.prev = tempPrev; |
| 31 | + //this.tail.prev.next = currentNode; |
| 32 | + currentNode.prev = tempPrevTail; |
| 33 | + currentNode.next = this.tail; |
| 34 | + this.tail.prev = currentNode; |
| 35 | + return this.hash[key].value; |
| 36 | + } |
44 | 37 |
|
45 | | -/** |
46 | | - * @param {number} key |
47 | | - * @param {number} value |
48 | | - * @return {void} |
49 | | - */ |
50 | | -LRUCache.prototype.put = function(key, value) { |
51 | | - let node = new Node(key, value); |
52 | | - if (this.hash[key]) { |
53 | | - node = this.tail.prev; |
54 | | - node.val = value; |
55 | | - } else if (this.count === this.capacity) { |
56 | | - const temp = this.head.next.next; |
57 | | - delete this.hash[this.head.next.key]; |
58 | | - this.head.next = temp; |
59 | | - temp.prev = this.head; |
60 | | - } else { |
61 | | - this.count++; |
| 38 | + return -1; |
62 | 39 | } |
63 | | - const temp = this.tail.prev; |
64 | | - temp.next = node; |
65 | | - node.prev = temp; |
66 | | - node.next = this.tail; |
67 | | - this.tail.prev = node; |
68 | | - this.hash[key] = node; |
69 | | - this.print(); |
70 | | -}; |
71 | 40 |
|
72 | | -LRUCache.prototype.moveToHead = function() {}; |
| 41 | + // put(key: number, value: number) { |
| 42 | + // let node = new ListNode({ key, value }); |
| 43 | + // if (this.hash[key]) { |
| 44 | + // node = this.tail.prev; |
| 45 | + // node.val = value; |
| 46 | + // } else if (this.count === this.capacity) { |
| 47 | + // const temp = this.head.next.next; |
| 48 | + // delete this.hash[this.head.next.key]; |
| 49 | + // this.head.next = temp; |
| 50 | + // temp.prev = this.head; |
| 51 | + // } else { |
| 52 | + // this.count++; |
| 53 | + // } |
| 54 | + // const temp = this.tail.prev; |
| 55 | + // temp.next = node; |
| 56 | + // node.prev = temp; |
| 57 | + // node.next = this.tail; |
| 58 | + // this.tail.prev = node; |
| 59 | + // this.hash[key] = node; |
| 60 | + // this.print(); |
| 61 | + // } |
| 62 | +} |
73 | 63 |
|
74 | | -LRUCache.prototype.print = function(message = "") { |
75 | | - let currentNode = this.head; |
76 | | - console.log("---- init ---"); |
77 | | - while (currentNode !== null) { |
78 | | - console.log(`${message}: [${currentNode.key}, ${currentNode.val}]`); |
79 | | - currentNode = currentNode.next; |
80 | | - } |
81 | | - console.log("---- end ---"); |
82 | | -}; |
| 64 | +// LRUCache.prototype.moveToHead = function () {}; |
| 65 | + |
| 66 | +// LRUCache.prototype.print = function (message = "") { |
| 67 | +// let currentNode = this.head; |
| 68 | +// console.log("---- init ---"); |
| 69 | +// while (currentNode !== null) { |
| 70 | +// console.log(`${message}: [${currentNode.key}, ${currentNode.val}]`); |
| 71 | +// currentNode = currentNode.next; |
| 72 | +// } |
| 73 | +// console.log("---- end ---"); |
| 74 | +// }; |
83 | 75 | /** |
84 | 76 | * Your LRUCache object will be instantiated and called as such: |
85 | 77 | * var obj = Object.create(LRUCache).createNew(capacity) |
86 | 78 | * var param_1 = obj.get(key) |
87 | 79 | * obj.put(key,value) |
88 | 80 | */ |
89 | 81 |
|
90 | | -const cache = new LRUCache(2); |
91 | | -cache.put(2, 1); |
92 | | -cache.put(2, 2); |
93 | | -cache.get(2); |
94 | | -cache.put(4, 1); |
95 | | -cache.put(1, 1); |
96 | | -cache.get(2); |
| 82 | +// const cache = new LRUCache(2); |
| 83 | +// cache.put(2, 1); |
| 84 | +// cache.put(2, 2); |
| 85 | +// cache.get(2); |
| 86 | +// cache.put(4, 1); |
| 87 | +// cache.put(1, 1); |
| 88 | +// cache.get(2); |
97 | 89 | // ["LRUCache","put","put","get","put","put","get"] |
98 | 90 | // [[2],[2,1],[2,2],[2],[1,1],[4,1],[2]] |
0 commit comments