当前位置:网站首页>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;
}
};
边栏推荐
- Learn FPGA from the bottom structure (16) -- customization and testing of pll/mmcm IP
- Leetcode customs clearance: hash table six, this is really a little simple
- 2022.7.24-----leetcode.1184
- MySQL 日期【加号/+】条件筛选问题
- [cloud native] use of Nacos taskmanager task management
- How to choose a microservice registration center?
- If the order is not paid for 30 minutes, it will be automatically cancelled. How to achieve this? (Collection Edition)
- [today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger
- 飞行器pid控制(旋翼飞控)
- 2022.7.24-----leetcode.1184
猜你喜欢

Solution to oom exceptions caused by improper use of multithreading in production environment (supreme Collection Edition)
![MySQL date [plus sign / +] condition filtering problem](/img/86/aed048e27b3e0b0baa919204bc067c.png)
MySQL date [plus sign / +] condition filtering problem
![[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference](/img/0f/8ce2d5487b16d38a152cfd3ab454bb.png)
[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference

JVM (XXIII) -- JVM runtime parameters

Key network protocols in tcp/ip four layer model

"Share" devaxpress asp Net v22.1 latest version system environment configuration requirements
![[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay](/img/18/b06e2e5a2f76dc2da1c2374b8424b3.png)
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
![[onnx] export pytorch model to onnx format: support multi parameter and dynamic input](/img/bd/e9a1d3a2c9343b75dbae5c7e18a87b.png)
[onnx] export pytorch model to onnx format: support multi parameter and dynamic input

【单细胞高级绘图】07.KEGG富集结果展示

Fanoutexchange switch code tutorial
随机推荐
Struct, enum type and union
Network RTK UAV test [easy to understand]
During the interview, I was asked how to remove the weight of MySQL, and who else wouldn't?
Follow up of Arlo's thinking
Rand1 generates rand9
securecrt乱码解决方法[通俗易懂]
FormatDateTime说解[通俗易懂]
[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day
Automated testing ----- selenium (I)
9. < tag dynamic programming and subsequence, subarray> lt.718. Longest repeated subarray + lt.1143. Longest common subsequence
[advanced mathematics] [4] indefinite integral
The database empties the table data and makes the primary key start from 1
Clickhouse notes 02 -- installation test clickvisual
Solution to oom exceptions caused by improper use of multithreading in production environment (supreme Collection Edition)
【高等数学】【5】定积分及应用
什么是聚类分析?聚类分析方法的类别[通俗易懂]
[advanced mathematics] [1] function, limit, continuity
Compilation and operation of program
If the order is not paid for 30 minutes, it will be automatically cancelled. How to achieve this? (Collection Edition)
Array of sword finger offer question bank summary (I) (C language version)