首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

删除Map()中最旧的条目,时间复杂度为O(1) JS

在JavaScript中,Map是一种数据结构,用于存储键值对的集合。如果要删除Map中最旧的条目,并且要求时间复杂度为O(1),可以使用以下方法:

  1. 使用Map结合双向链表:创建一个Map对象,并在每个键值对中添加一个时间戳属性。同时,使用一个双向链表来维护键值对的顺序,最旧的条目位于链表的头部。这样,当需要删除最旧的条目时,只需通过链表头部获取最旧的键,然后通过Map的delete方法删除该键值对。由于链表头部的访问是O(1)的操作,所以删除操作的时间复杂度为O(1)。

以下是一个示例代码:

代码语言:txt
复制
class LRUCache {
  constructor(capacity) {
    this.capacity = capacity;
    this.map = new Map();
    this.head = null;
    this.tail = null;
  }

  get(key) {
    if (this.map.has(key)) {
      const node = this.map.get(key);
      this.moveToHead(node);
      return node.value;
    }
    return -1;
  }

  put(key, value) {
    if (this.map.has(key)) {
      const node = this.map.get(key);
      node.value = value;
      this.moveToHead(node);
    } else {
      const newNode = { key, value, prev: null, next: null };
      if (this.map.size >= this.capacity) {
        this.removeTail();
      }
      this.addToHead(newNode);
      this.map.set(key, newNode);
    }
  }

  moveToHead(node) {
    if (node === this.head) {
      return;
    }
    if (node.prev) {
      node.prev.next = node.next;
    }
    if (node.next) {
      node.next.prev = node.prev;
    }
    if (node === this.tail) {
      this.tail = node.prev;
    }
    node.prev = null;
    node.next = this.head;
    if (this.head) {
      this.head.prev = node;
    }
    this.head = node;
  }

  removeTail() {
    if (!this.tail) {
      return;
    }
    this.map.delete(this.tail.key);
    if (this.tail.prev) {
      this.tail.prev.next = null;
    } else {
      this.head = null;
    }
    this.tail = this.tail.prev;
  }

  addToHead(node) {
    node.next = this.head;
    if (this.head) {
      this.head.prev = node;
    }
    this.head = node;
    if (!this.tail) {
      this.tail = node;
    }
  }
}

const cache = new LRUCache(5);
cache.put("key1", "value1");
cache.put("key2", "value2");
cache.put("key3", "value3");
cache.put("key4", "value4");
cache.put("key5", "value5");

console.log(cache.get("key3")); // 输出:value3
console.log(cache.get("key1")); // 输出:value1

cache.put("key6", "value6"); // 删除最旧的条目

console.log(cache.get("key2")); // 输出:-1,已被删除
console.log(cache.get("key6")); // 输出:value6

在上述示例代码中,LRUCache类实现了一个基于LRU(最近最少使用)算法的缓存。LRUCache类使用了Map来存储键值对,并使用双向链表来维护键值对的顺序。通过在每个键值对中添加时间戳属性,并使用双向链表来维护顺序,可以实现删除最旧条目的O(1)时间复杂度。

腾讯云相关产品和产品介绍链接地址:

  • 云服务器(CVM):提供可扩展的计算容量,满足各种业务需求。产品介绍链接
  • 云数据库 MySQL 版(CMYSQL):高性能、可扩展的关系型数据库服务。产品介绍链接
  • 云存储(COS):安全可靠、高扩展性的云端存储服务。产品介绍链接
  • 人工智能(AI):提供丰富的人工智能服务和解决方案,包括图像识别、语音识别、自然语言处理等。产品介绍链接
  • 物联网(IoT):提供全面的物联网解决方案,包括设备接入、数据管理、应用开发等。产品介绍链接
  • 云原生应用引擎(TKE):用于构建和管理容器化应用的托管服务。产品介绍链接
  • 腾讯云区块链服务(TBC):提供一站式区块链解决方案,包括区块链网络搭建、智能合约开发等。产品介绍链接
  • 腾讯云元宇宙(Tencent Cloud Metaverse):提供全面的元宇宙解决方案,包括虚拟现实、增强现实、三维建模等。产品介绍链接
页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

3分23秒

2.12.使用分段筛的最长素数子数组

5分12秒

2.7.素性检验之孙达拉姆筛sieve of sundaram

5分39秒

2.10.素性检验之分段筛segmented sieve

5分8秒

084.go的map定义

34分39秒

2.4.素性检验之欧拉筛sieve of euler

1分21秒

2.9.素性检验之按位筛bitwise sieve

7分58秒
5分10秒

2.18.索洛瓦-施特拉森素性测试Solovay-Strassen primality test

7分18秒

1.6.线性打表求逆元

22分1秒

1.7.模平方根之托内利-香克斯算法Tonelli-Shanks二次剩余

12分23秒

1.8.模平方根之奇波拉算法Cipolla二次剩余

15分29秒

1.9.模立方根之佩拉尔塔算法Peralta三次剩余

领券