当前位置:网站首页>leetcode-6130:设计数字容器系统
leetcode-6130:设计数字容器系统
2022-07-25 20:36:00 【菊头蝙蝠】
leetcode-6130:设计数字容器系统
题目
设计一个数字容器系统,可以实现以下功能:
- 在系统中给定下标处 插入 或者 替换 一个数字。
- 返回 系统中给定数字的最小下标。
请你实现一个 NumberContainers 类:
- NumberContainers() 初始化数字容器系统。
- void change(int index, int number) 在下标 index 处填入 number 。如果该下标 index 处已经有数字了,那么用 number 替换该数字。
- int find(int number) 返回给定数字 number 在系统中的最小下标。如果系统中没有 number ,那么返回 -1 。
示例:
输入:
["NumberContainers", "find", "change", "change", "change", "change", "find", "change", "find"]
[[], [10], [2, 10], [1, 10], [3, 10], [5, 10], [10], [1, 20], [10]]
输出:
[null, -1, null, null, null, null, 1, null, 2]
解释:
NumberContainers nc = new NumberContainers();
nc.find(10); // 没有数字 10 ,所以返回 -1 。
nc.change(2, 10); // 容器中下标为 2 处填入数字 10 。
nc.change(1, 10); // 容器中下标为 1 处填入数字 10 。
nc.change(3, 10); // 容器中下标为 3 处填入数字 10 。
nc.change(5, 10); // 容器中下标为 5 处填入数字 10 。
nc.find(10); // 数字 10 所在的下标为 1 ,2 ,3 和 5 。因为最小下标为 1 ,所以返回 1 。
nc.change(1, 20); // 容器中下标为 1 处填入数字 20 。注意,下标 1 处之前为 10 ,现在被替换为 20 。
nc.find(10); // 数字 10 所在下标为 2 ,3 和 5 。最小下标为 2 ,所以返回 2 。

解题
方法一:哈希map+有序集合set
建立 数字—>索引 的哈希map,这样在查找的时候,就可以直接找到
那么如果取到最小的索引呢?可以使用有序集合set,那么set.begin()指向的元素就是最小的索引。
因此构建 unordered_map<int,set<int>> ,可以稳定O(1)复杂度完成查找
class NumberContainers {
public:
unordered_map<int,int> index2num;//索引--->数字
unordered_map<int,set<int>> num2index;//数字--->索引
NumberContainers() {
}
void change(int index, int number) {
if(index2num.count(index)==0){
//新插入的索引,直接建立map就行。
index2num[index]=number;
num2index[number].insert(index);
}else{
//如果索引之前存在
//先删除旧的索引
int oldNum=index2num[index];
num2index[oldNum].erase(index);
if(num2index[oldNum].empty()){
num2index.erase(oldNum);
}
//插入新的索引
index2num[index]=number;
num2index[number].insert(index);
}
}
int find(int number) {
if(num2index.count(number)){
return *num2index[number].begin();
}
return -1;
}
};
边栏推荐
- [advanced mathematics] [6] differential calculus of multivariate functions
- Solution to oom exceptions caused by improper use of multithreading in production environment (supreme Collection Edition)
- [today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging
- 每条你收藏的资讯背后,都离不开TA
- Online XML to JSON tool
- ROS_ Rqt toolbox
- [today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now
- Automated testing ----- selenium (I)
- Arrow parquet
- 【高等数学】【1】函数、极限、连续
猜你喜欢

Google guava is just a brother. What is the real king of caching? (glory Collection Edition)

Advantages of network virtualization of various manufacturers
![[advanced mathematics] [4] indefinite integral](/img/4f/2aae654599fcc0ee85cb1ba46c9afd.png)
[advanced mathematics] [4] indefinite integral

数据库清空表数据并让主键从1开始

【高等数学】【3】微分中值定理与导数的应用

"Share" devaxpress asp Net v22.1 latest version system environment configuration requirements

Remote monitoring solution of intelligent electronic boundary stake Nature Reserve
![[paper reading] unpaired image to image translation using cycle consistent advantageous networks](/img/73/69651dd8ecfdddd1cae13a1d223d51.png)
[paper reading] unpaired image to image translation using cycle consistent advantageous networks

Prescan quick start to master Lesson 19: prescan actuator configuration, track synchronization and non configuration of multiple tracks

103. (cesium chapter) cesium honeycomb diagram (square)
随机推荐
DIY个人服务器(diy存储服务器)
Docker 搭建 Redis Cluster集群
Apache Mina framework "suggestions collection"
[MCU] 51 MCU burning those things
Socket error Event: 32 Error: 10053. Connection closing...Socket close
"Share" devaxpress asp Net v22.1 latest version system environment configuration requirements
【NOI模拟赛】字符串匹配(后缀自动机SAM,莫队,分块)
Leetcode customs clearance: hash table six, this is really a little simple
【高等数学】【6】多元函数微分学
【ONNX】pytorch模型导出成ONNX格式:支持多参数与动态输入
[Infographics Show] 248 Public Domain Name
Online random coin tossing positive and negative statistical tool
智能电子界桩自然保护区远程监控解决方案
The database empties the table data and makes the primary key start from 1
第六章 修改规范(SPEC)类
LeetCode通关:哈希表六连,这个还真有点简单
[today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
Rand1 generates rand9
雷达水位计的工作原理及安装维护注意事项
Introduction to several scenarios involving programming operation of Excel in SAP implementation project