C++ | Leetcode C++题解之第432题全O(1)的数据结构

news/2024/9/24 6:14:16/

题目:

题解

class AllOne {list<pair<unordered_set<string>, int>> lst;unordered_map<string, list<pair<unordered_set<string>, int>>::iterator> nodes;public:AllOne() {}void inc(string key) {if (nodes.count(key)) {auto cur = nodes[key], nxt = next(cur);if (nxt == lst.end() || nxt->second > cur->second + 1) {unordered_set<string> s({key});nodes[key] = lst.emplace(nxt, s, cur->second + 1);} else {nxt->first.emplace(key);nodes[key] = nxt;}cur->first.erase(key);if (cur->first.empty()) {lst.erase(cur);}} else { // key 不在链表中if (lst.empty() || lst.begin()->second > 1) {unordered_set<string> s({key});lst.emplace_front(s, 1);} else {lst.begin()->first.emplace(key);}nodes[key] = lst.begin();}}void dec(string key) {auto cur = nodes[key];if (cur->second == 1) { // key 仅出现一次,将其移出 nodesnodes.erase(key);} else {auto pre = prev(cur);if (cur == lst.begin() || pre->second < cur->second - 1) {unordered_set<string> s({key});nodes[key] = lst.emplace(cur, s, cur->second - 1);} else {pre->first.emplace(key);nodes[key] = pre;}}cur->first.erase(key);if (cur->first.empty()) {lst.erase(cur);}}string getMaxKey() {return lst.empty() ? "" : *lst.rbegin()->first.begin();}string getMinKey() {return lst.empty() ? "" : *lst.begin()->first.begin();}
};

http://www.ppmy.cn/news/1529685.html

相关文章

.NET 6.0 MVC使用Cookie进行身份验证

一般而言MVC 是不与其他系统发生数据交互&#xff0c;所以使用Cookie验证即可&#xff0c;无需安装拓展。 1.Program里面注册服务 //1.选择使用那种方式来身份验证 builder.Services.AddAuthentication(option > {option.DefaultAuthenticateScheme CookieAuthentication…

Protobuf:基本概念与使用流程

Protobuf&#xff1a;基本概念与使用流程 基本概念Linux 安装使用流程.proto文件编译使用 运行机制 基本概念 在进行网络编程时&#xff0c;经常需要进行数据传输&#xff0c;只有双方主机都保证数据格式的一致性&#xff0c;才能保证数据被正常解析。这个过程称为序列化与反序…

PDF里怎么直接编辑文字?简单操作指南

PDF作为一种广泛使用的文档格式&#xff0c;因其稳定性和跨平台兼容性而受到欢迎。然而&#xff0c;PDF原生的编辑功能相对有限&#xff0c;尤其是直接编辑其中的文字。但幸运的是&#xff0c;随着技术的发展&#xff0c;我们现在有几种方法可以在PDF中直接编辑文字。在本文中&…

JS巧用.padStart()|.padEnd()方法用另一个字符串填充当前字符串

示例 const sortNum computed(() > No.${${1}.padStart(2, 0)}); // No.01const sortNum computed(() > No.${${1}.padEnd(2, 0)}); // No.10 padStart .padStart() 方法可以用于添加一个字符串作为填充&#xff0c;以使当前字符串达到所需的长度。例如&#xff0c;…

Vue开发前端图片上传给java后端

前端效果图 图片上传演示 1 前端代码 <template><div><!-- 页面标题 --><h1 class"page-title">图片上传演示</h1><div class"upload-container"><!-- 使用 van-uploader 组件进行文件上传&#xff0c;v-model 绑…

开关电源自动测试系统的测试设备与特色

突破传统测试系统的操作维护困难等限制&#xff0c;NSAT-8000开关电源自动测试系统以其开放式架构和0代码模式&#xff0c;带来了不一样的开关电源自动化测试体验。 开关电源自动测试系统的测试设备 开关电源自动测试系统核心硬件包括&#xff1a;可编程交直流电源、电子负载、…

【技术解析】wx.request 封装:优化小程序网络请求的最佳实践

在当今的小程序开发领域&#xff0c;网络请求是构建动态应用的核心。微信小程序提供的 wx.request API 虽然强大&#xff0c;但在面对复杂业务逻辑时&#xff0c;其直接使用方式可能会带来一系列问题。本文将深入探讨封装 wx.request 的必要性&#xff0c;并提供一套实用的封装…

java.nio.ByteBuffer的 capacity, limit, position, mark

java.nio.ByteBuffer的 capacity, limit, position, mark Capacity&#xff08;容量&#xff09; 定义&#xff1a;缓冲区的总容量&#xff0c;即缓冲区中可以容纳的元素的数量。这个容量在缓冲区创建时被设定&#xff0c;并且之后不能被改变。 用途&#xff1a;它定义了缓冲区…