NC93 设计LRU缓存结构

描述

设计LRU(最近最少使用)缓存结构,该结构在构造时确定大小,假设大小为K,并有如下两个功能

  1. set(key, value):将记录(key, value)插入该结构
  2. get(key):返回key对应的value值

提示:

  1. 某个key的set或get操作一旦发生,认为这个key的记录成了最常使用的,然后都会刷新缓存。
  2. 当缓存的大小超过K时,移除最不经常使用的记录。
  3. 输入一个二维数组与K,二维数组每一维有2个或者3个数字,第1个数字为opt,第2,3个数字为key,value
    若opt=1,接下来两个整数key, value,表示set(key, value)
    若opt=2,接下来一个整数key,表示get(key),若key未出现过或已被移除,则返回-1
    对于每个opt=2,输出一个答案
  4. 为了方便区分缓存里key与value,下面说明的缓存里key用""号包裹

进阶:你是否可以在O(1)的时间复杂度完成set和get操作

在这里插入图片描述

/**
 * lru design
 * @param operators int整型二维数组 the ops
 * @param k int整型 the k
 * @return int整型一维数组
 */
function LRU( operators ,  k ) {
    // write code here
    const n = operators.length
    const map = new Map()
    const res = []
    for (let i = 0; i < n; i++) {
        if (operators[i][0] === 1) {
            set(operators[i][1], operators[i][2])
        } else {
            res.push(get(operators[i][1]))
        }
    }
    
    function get (key) {
        const val = map.get(key)
        if (val === undefined) {
            return -1
        }
        map.delete(key)
        map.set(key, val)
        return val
    }
    
    function set(key, val) {
        if (map.has(key)) {
            map.delete(key)
        }
        map.set(key, val)
        if (map.size > k){
            map.delete(map.keys().next().value)
        }
    }
    return res
}

module.exports = {
    LRU : LRU
};

在这里插入图片描述

another methods

/**
 * lru design
 * @param operators int整型二维数组 the ops
 * @param k int整型 the k
 * @return int整型一维数组
 */
function LRU( operators ,  k ) {
    // write code here
    const n = operators.length
    this.k = k
    this.map = new Map()
    this.get = function (key) {
        const val = this.map.get(key)
        if (val === undefined) {
            return -1
        }
        this.map.delete(key)
        this.map.set(key, val)
        return val
    }
    this.set = function (key, val) {
        if (this.map.has(key)) {
            this.map.delete(key)
        }
        this.map.set(key, val)
        if (this.map.size > this.k){
            this.map.delete(this.map.keys().next().value)
        }
    }
    const res = []
    for (let i = 0; i < n; i++) {
        if (operators[i][0] === 1) {
            this.set(operators[i][1], operators[i][2])
        } else {
            res.push(this.get(operators[i][1]))
        }
    }
    return res
}
module.exports = {
    LRU : LRU
};
这种写法就不行

似乎牛客只会调用函数LRU(),并不会 new LRU(),然后在使用创建的对象去调用方法。

/**
 * lru design
 * @param operators int整型二维数组 the ops
 * @param k int整型 the k
 * @return int整型一维数组
 */
function LRU( operators ,  k ) {
    // write code here
    const n = operators.length
    this.k = k
    this.map = new Map()
    const res = []
    for (let i = 0; i < n; i++) {
        if (operators[i][0] === 1) {
            this.set(operators[i][1], operators[i][2])
        } else {
            res.push(this.get(operators[i][1]))
        }
    }
    return res
}

LRU.prototype.get = function (key) {
        const val = this.map.get(key)
        if (val === undefined) {
            return -1
        }
        this.map.delete(key)
        this.map.set(key, val)
        return val
}
LRU.prototype.set = function (key, val) {
        if (this.map.has(key)) {
            this.map.delete(key)
        }
        this.map.set(key, val)
        if (this.map.size > this.k){
            this.map.delete(this.map.keys().next().value)
        }
    }

module.exports = {
    LRU : LRU
};
分析原因
<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>Document</title>
  <style>

  </style>
</head>

<body>
  <script>
    function LRU(operators, k) {
      // write code here
      const n = operators.length
      this.k = k
      this.map = new Map()
      const res = []
      for (let i = 0; i < n; i++) {
        if (operators[i][0] === 1) {
          this.set(operators[i][1], operators[i][2])
        } else {
          res.push(this.get(operators[i][1]))
        }
      }
      return res
    }

    LRU.prototype.get = function (key) {
      const val = this.map.get(key)
      if (val === undefined) {
        return -1
      }
      this.map.delete(key)
      this.map.set(key, val)
      return val
    }
    LRU.prototype.set = function (key, val) {
      if (this.map.has(key)) {
        this.map.delete(key)
      }
      this.map.set(key, val)
      if (this.map.size > this.k) {
        this.map.delete(this.map.keys().next().value)
      }
    }

    // const ldh = new LRU([[1, 1, 1], [1, 2, 2], [1, 3, 2], [2, 1], [1, 4, 4], [2, 2]], 3) // 这样处理的话LRU就是一个构造函数
    // console.log(ldh)
    console.log(LRU([[1,1,1],[1,2,2],[1,3,2],[2,1],[1,4,4],[2,2]],3)) // 这样处理会报错,因为这样处理的话LRU就是一个普通的函数,而非构造函数,它会报错 Uncaught TypeError: this.set is not a function
  </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值