当前位置:网站首页>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;
}
};
边栏推荐
- qml 结合 QSqlTableModel 动态加载数据 MVC「建议收藏」
- 移动web布局方法
- [leetcode] 28. Implement strstr ()
- Technology cloud report: more than zero trust, the wild hope of Parra's "Digital Security Cloud strategy"
- Three skills of interface request merging, and the performance is directly exploded!
- Proxy实现mysql读写分离
- tga文件格式(波形声音文件格式)
- Docker 搭建 Redis Cluster集群
- Introduction and construction of consul Registration Center
- "Share" devaxpress asp Net v22.1 latest version system environment configuration requirements
猜你喜欢

Clickhouse notes 02 -- installation test clickvisual
![[today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger](/img/14/f2b68dbe4e6a9b8d89ed9ff38f5e11.png)
[today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger

LeetCode通关:哈希表六连,这个还真有点简单

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

Advantages of network virtualization of various manufacturers

ROS_ Rqt toolbox

Timing analysis and constraints based on xlinx (1) -- what is timing analysis? What are temporal constraints? What is temporal convergence?

Has baozi ever played in the multi merchant system?

Go language go language built-in container

Kubernetes进阶部分学习笔记
随机推荐
qml 结合 QSqlTableModel 动态加载数据 MVC「建议收藏」
Docker builds redis cluster
String of sword finger offer question bank summary (II) (C language version)
[advanced mathematics] [4] indefinite integral
9. < tag dynamic programming and subsequence, subarray> lt.718. Longest repeated subarray + lt.1143. Longest common subsequence
第六章 修改规范(SPEC)类
Do you still have certificates to participate in the open source community?
[today in history] July 7: release of C; Chrome OS came out; "Legend of swordsman" issued
Network RTK UAV test [easy to understand]
【高等数学】【4】不定积分
QML combines qsqltablemodel to dynamically load data MVC "recommended collection"
【TensorRT】动态batch进行推理
[paper reading] unpaired image to image translation using cycle consistent advantageous networks
【高等数学】【6】多元函数微分学
股票软件开发
Advantages of network virtualization of various manufacturers
[today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
[advanced mathematics] [5] definite integral and its application
[onnx] export pytorch model to onnx format: support multi parameter and dynamic input
[leetcode] 28. Implement strstr ()