当前位置:网站首页>剑指offer专项突击版第10天
剑指offer专项突击版第10天
2022-07-25 13:14:00 【hys__handsome】
剑指 Offer II 030. 插入、删除和随机访问都是 O(1) 的容器
变长数组(随机访问)+哈希表( O ( 1 ) O(1) O(1)增、删)
class RandomizedSet {
private:
unordered_map<int,int> um;
vector<int> nums;
public:
unordered_set<int> us;
/** Initialize your data structure here. */
RandomizedSet() {
srand((unsigned)time(nullptr));
}
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
bool insert(int val) {
if(um.count(val)) return false;
nums.push_back(val);
um[val] = nums.size()-1;
return true;
}
/** Removes a value from the set. Returns true if the set contained the specified element. */
bool remove(int val) {
if(!um.count(val)) return false;
int idx = um[val];
nums[idx] = nums.back();
um[nums.back()] = idx;
nums.pop_back();
um.erase(val);
return true;
}
/** Get a random element from the set. */
int getRandom() {
return nums[rand()%nums.size()];
}
};
双向链表+哈希表
有使用(get、put)的结点添加到头结点,如果容量不够只需删除尾部结点即可
class LRUCache {
private:
struct DLinkedNode {
int key,value;
DLinkedNode *next, *prev;
}*head, *tail;
unordered_map<int,DLinkedNode*> um;
int capacity;
public:
LRUCache(int capacity) {
this->capacity = capacity;
head = new DLinkedNode();
tail = new DLinkedNode();
head->next = tail;
tail->prev = head;
}
int get(int key) {
if(um.count(key)) {
move_to_head(um[key]);
return um[key]->value;
} else {
return -1;
}
}
void put(int key, int value) {
if(um.count(key)) {
um[key]->value = value;
move_to_head(um[key]);
} else {
DLinkedNode *node = new DLinkedNode();
node->value = value;
node->key = key;
add_to_head(node);
um[key]= node;
if(um.size() > capacity) {
um.erase(tail->prev->key);
delete remove_node(tail->prev); //防止内存溢出
}
}
}
void add_to_head(DLinkedNode *node) {
node->next = head->next;
node->prev = head;
head->next->prev = node;
head->next = node;
}
void move_to_head(DLinkedNode *node) {
remove_node(node);
add_to_head(node);
}
DLinkedNode* remove_node(DLinkedNode *node){
node->prev->next = node->next;
node->next->prev = node->prev;
return node;
}
};
边栏推荐
- Memory layout of program
- Friends let me see this code
- Common operations for Yum and VIM
- 安装mujoco报错:distutils.errors.DistutilsExecError: command ‘gcc‘ failed with exit status 1
- 机器学习强基计划0-4:通俗理解奥卡姆剃刀与没有免费午餐定理
- vim基础操作汇总
- 全球都热炸了,谷歌服务器已经崩掉了
- Pytorch creates its own dataset and loads the dataset
- 工业互联网的内涵及其应用
- 并发编程之AQS
猜你喜欢

Friends let me see this code

Common operations for Yum and VIM

B树和B+树

为提高效率使用ParallelStream竟出现各种问题

MLIR原理与应用技术杂谈

录制和剪辑视频,如何解决占用空间过大的问题?

Cv2.resize function reports an error: error: (-215:assertion failed) func= 0 in function ‘cv::hal::resize‘

【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享

How to solve the problem of taking up too much space when recording and editing videos?

并发编程 — 内存模型 JMM
随机推荐
Pytorch creates its own dataset and loads the dataset
MLIR原理与应用技术杂谈
VIM basic operation summary
Azure Devops(十四) 使用Azure的私有Nuget仓库
好友让我看这段代码
Based on Baiwen imx6ull_ Pro development board transplants LCD multi touch driver (gt911)
【AI4Code】《IntelliCode Compose: Code Generation using Transformer》 ESEC/FSE 2020
【GCN-RS】Learning Explicit User Interest Boundary for Recommendation (WWW‘22)
Convolutional neural network model -- lenet network structure and code implementation
【重温SSM框架系列】15 - SSM系列博文总结【SSM杀青篇】
vim基础操作汇总
全球都热炸了,谷歌服务器已经崩掉了
【GCN】《Adaptive Propagation Graph Convolutional Network》(TNNLS 2020)
[figure attack and Defense] backdoor attacks to graph neural networks (sacmat '21)
Atcoder beginer contest 261 f / / tree array
Design and principle of thread pool
Convolutional neural network model -- alexnet network structure and code implementation
[CSDN year-end summary] end and start, always on the way - "2021 summary of" 1+1= Wang "
Shell common script: check whether a domain name and IP address are connected
yum和vim须掌握的常用操作