Title: #146 - LRU Cache - LeetCode JavaScript Solutions
Open Graph Title: #146 - LRU Cache - LeetCode JavaScript Solutions
X Title: #146 - LRU Cache - LeetCode JavaScript Solutions
Description: Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. Implement the LRUCache class: LRUCache(int capac...
Open Graph Description: Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. Implement the LRUCache class: LRUCache(int capac...
X Description: Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. Implement the LRUCache class: LRUCache(int capac...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/lru-cache
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/lru-cache","name":"#146 - LRU Cache - LeetCode JavaScript Solutions","description":"Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. Implement the LRUCache class: LRUCache(int capac...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number} capacity\n */\nvar LRUCache = function(capacity) {\n this.cache = new Map();\n this.capacity = capacity;\n};\n\n/**\n * @param {number} key\n * @return {number}\n */\nLRUCache.prototype.get = function(key) {\n if (!this.cache.has(key)) {\n return -1;\n }\n\n const value = this.cache.get(key);\n this.cache.delete(key);\n this.cache.set(key, value);\n return this.cache.get(key);\n};\n\n/**\n * @param {number} key\n * @param {number} value\n * @return {void}\n */\nLRUCache.prototype.put = function(key, value) {\n if (this.cache.has(key)) {\n this.cache.delete(key);\n }\n this.cache.set(key, value);\n if (this.cache.size > this.capacity) {\n this.cache.delete(this.cache.keys().next().value);\n }\n};","keywords":"LeetCode 146, #146 - LRU Cache, Medium, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/lru-cache |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/lru-cache/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0146-lru-cache.js |
| Hash Table | https://leetcodejavascript.com/tags/hash-table |
| Design | https://leetcodejavascript.com/tags/design |
| Linked List | https://leetcodejavascript.com/tags/linked-list |
| Doubly-Linked List | https://leetcodejavascript.com/tags/doubly-linked-list |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width,initial-scale=1
Robots: index, follow