当前位置:网站首页>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;
}
};
边栏推荐
- Docker 搭建 Redis Cluster集群
- preprocessor directives
- Has baozi ever played in the multi merchant system?
- 第六章 修改规范(SPEC)类
- Proxy implements MySQL read / write separation
- Cloud native guide: what is cloud native infrastructure
- What is cluster analysis? Categories of cluster analysis methods [easy to understand]
- Advantages of network virtualization of various manufacturers
- Recommended books | essentials of industrial digital transformation: methods and Practice
- Interpretation of filter execution sequence source code in sprigboot
猜你喜欢

网络协议:TCP Part2

If the order is not paid for 30 minutes, it will be automatically cancelled. How to achieve this? (Collection Edition)
![[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day

Chinese son-in-law OTA Ono became the first Asian president of the University of Michigan, with an annual salary of more than 6.5 million!

JVM (XXIII) -- JVM runtime parameters

从底层结构开始学习FPGA(16)----PLL/MMCM IP的定制与测试
![[advanced mathematics] [6] differential calculus of multivariate functions](/img/9e/84fe6f74b58cbaabab1b6eed0df556.png)
[advanced mathematics] [6] differential calculus of multivariate functions

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

Compilation and operation of program

During the interview, I was asked how to remove the weight of MySQL, and who else wouldn't?
随机推荐
雷达水位计的工作原理及安装维护注意事项
Card link
FormatDateTime说解[通俗易懂]
Key network protocols in tcp/ip four layer model
【高等数学】【6】多元函数微分学
How much memory does bitmap occupy in the development of IM instant messaging?
JS scope and scope chain
Proxy implements MySQL read / write separation
【TensorRT】动态batch进行推理
Introduction and construction of consul Registration Center
Vivo official website app full model UI adaptation scheme
The uniapp project starts with an error binding Node is not a valid Win32 Application ultimate solution
【TensorRT】trtexec工具转engine
[today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now
Leetcode customs clearance: hash table six, this is really a little simple
Remote monitoring solution of intelligent electronic boundary stake Nature Reserve
JS作用域与作用域链
DIY个人服务器(diy存储服务器)
【高等数学】【1】函数、极限、连续
Advantages of network virtualization of various manufacturers