当前位置:网站首页>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;
}
};
边栏推荐
- HTTP cache tongtianpian, there may be something you want
- Django 2 ----- database and admin
- hcip第八天笔记
- Introduction and features of numpy (I)
- How to solve the problem of taking up too much space when recording and editing videos?
- Online Learning and Pricing with Reusable Resources: Linear Bandits with Sub-Exponential Rewards: Li
- Concurrent programming - memory model JMM
- Leetcode 113. 路径总和 II
- 互斥锁、自旋锁、读写锁……理清它们的区别和应用
- 程序员成长第二十七篇:如何评估需求优先级?
猜你喜欢

0715RHCSA

Convolutional neural network model -- vgg-16 network structure and code implementation

错误: 找不到或无法加载主类 xxxx

Install mujoco and report an error: distutils.errors DistutilsExecError: command ‘gcc‘ failed with exit status 1

并发编程 — 内存模型 JMM

领域驱动模型设计与微服务架构落地-模型设计

6W+字记录实验全过程 | 探索Alluxio经济化数据存储策略

0710RHCSA

嵌入式代码如何进行重构?

【GCN-RS】Towards Representation Alignment and Uniformity in Collaborative Filtering (KDD‘22)
随机推荐
Based on Baiwen imx6ull_ Ap3216 experiment driven by Pro development board
hcip第六天笔记
面试官问我:Mysql的存储引擎你了解多少?
Esp32-c3 is based on blinker lighting control 10 way switch or relay group under Arduino framework
My creation anniversary
TCP的拥塞控制
【CTR】《Towards Universal Sequence Representation Learning for Recommender Systems》 (KDD‘22)
基于百问网IMX6ULL_PRO开发板移植LCD多点触摸驱动(GT911)
Hcip eighth day experiment
hcip第八天笔记
【AI4Code】《Pythia: AI-assisted Code Completion System》(KDD 2019)
stable_baselines快速入门
Hcip seventh day notes
说说对hashcode和equals方法的理解?
QGIS loading online map: Gaode, Tiandi map, etc
Uniapp handles background transfer pictures
0715RHCSA
Mujoco+spinningup for intensive learning training quick start
Based on Baiwen imx6ull_ Pro development board transplants LCD multi touch driver (gt911)
外围系统调用SAP的WebAPI接口