当前位置:网站首页>Sword finger offer special assault edition day 10
Sword finger offer special assault edition day 10
2022-07-25 13:31:00 【hys__ handsome】
The finger of the sword Offer II 030. Insert 、 Deletion and random access are O(1) The container of
Variable length array ( Random access )+ Hashtable ( O ( 1 ) O(1) O(1) increase 、 Delete )
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()];
}
};
The finger of the sword Offer II 031. At least use cache recently
Double linked list + Hashtable
Have use (get、put) Add the node of to the head node , If the capacity is not enough, just delete the tail node
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); // Prevent memory overflow
}
}
}
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;
}
};
边栏推荐
- Azure Devops(十四) 使用Azure的私有Nuget仓库
- Shell common script: get the IP address of the network card
- Hcip day 9 notes
- The simplest solution of the whole network 1045 access denied for user [email protected] (using password:YES)
- 面试官问我:Mysql的存储引擎你了解多少?
- Uniapp handles background transfer pictures
- 说说对hashcode和equals方法的理解?
- 2022全球开发者中,你的收入排多少?
- 二叉树基本知识
- 卷积神经网络模型之——LeNet网络结构与代码实现
猜你喜欢

Brpc source code analysis (III) -- the mechanism of requesting other servers and writing data to sockets

Excel record macro

Excel add key run macro

【CTR】《Towards Universal Sequence Representation Learning for Recommender Systems》 (KDD‘22)

好友让我看这段代码

The whole process of 6w+ word recording experiment | explore the economical data storage strategy of alluxio

【AI4Code】《IntelliCode Compose: Code Generation using Transformer》 ESEC/FSE 2020

Excel录制宏

面试官问我:Mysql的存储引擎你了解多少?
![[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing](/img/6e/9e0abf8db5ec93080033bd89605ac2.jpg)
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
随机推荐
How to realize the configuration method of user password free login?
hcip第八天笔记
电脑里一辈子都不想删的神仙软件
并发编程 — 内存模型 JMM
若依如何实现用户免密登录配置方法?
Leetcode 113. 路径总和 II
Simple understanding of flow
[CSDN year-end summary] end and start, always on the way - "2021 summary of" 1+1= Wang "
Docker learning - redis cluster -3 master and 3 slave - capacity expansion - capacity reduction building
互斥锁、自旋锁、读写锁……理清它们的区别和应用
【GCN】《Adaptive Propagation Graph Convolutional Network》(TNNLS 2020)
错误: 找不到或无法加载主类 xxxx
Shell common script: check whether a domain name and IP address are connected
并发编程之AQS
QingChuang technology joined dragon lizard community to build a new ecosystem of intelligent operation and maintenance platform
0720RHCSA
vim基础操作汇总
IM系统-消息流化一些常见问题
Mutex lock, spin lock, read-write lock... Clarify their differences and applications
Date and time function of MySQL function summary