/*** @param {number} capacity*/var LRUCache = function(capacity) {this.size = capacitythis.map = new Map()};/*** @param {number} key* @return {number}*/LRUCache.prototype.get = function(key) {let isExist = this.map.has(key)if (!isExist) return -1let item = this.map.get(key)this.map.delete(key)this.map.set(key, item)return item};/*** @param {number} key* @param {number} value* @return {void}*/LRUCache.prototype.put = function(key, value) {let isExit = this.map.has(key, value)if (isExit) {this.map.delete(key)}this.map.set(key, value)if (this.map.size > this.size) {// 这里有个知识点// map的entries方法,还有keys方法(可以看mdn)),会返回一个迭代器// 迭代器调用next也是顺序返回,所以返回第一个的值就是最老的,找到并删除即可this.map.delete(this.map.entries().next().value[0])}return null};
本次思路是没问题的 ,不过 this.map.entries().next().value[0] 这个不熟悉导致写了很多for循环和中间变量来存储导致耗时
