当前位置:网站首页>Force button brush question 01 (reverse linked list + sliding window +lru cache mechanism)
Force button brush question 01 (reverse linked list + sliding window +lru cache mechanism)
2022-07-04 15:04:00 【sun*san】
I haven't written a summary for a long time , Then I was busy writing projects some time ago , I seldom brush questions , Let's write an explanation today .
Reverse a linked list
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* p;
p=head;
ListNode* q=nullptr;
while(p){
ListNode* next = p->next;// Record the next digit of the current position
p->next=q;// Link the chain of the current position to the previous position
q=p;// Update previous
p=next;// Move to the next position to continue the previous operation
}
return q;
}
};
The sliding window
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if(s==""){// It is specially judged that there is no character
return 0;
}
map<char,int>a;
int sum=0;
int flag;
for(int i=0,j=0;j<s.length();j++){
if(a.find(s[j])!=a.end()){// If there are the same characters , to update i Subscript
i=max(a[s[j]],i);
}
sum=max(sum,j-i+1);// to update sum value
a[s[j]]=j+1;// take value The value is set to the character subscript +1, Make it easy to get the character position
}
return sum;
}
};Violence at first , It's not right , Time overrun , Then we use window sliding , By updating the i,j Two subscripts to find the maximum length

struct Link{// Create a bidirectional linked list node
int k,v;
struct Link* prev,*next;
};
class LRUCache {
private:
int Maxsize;
int size=0;
struct Link*head,*tail;
map<int,Link*>num;
public:
LRUCache(int capacity) {
head=new Link();// Bidirectional list initialization
tail=new Link();
head->next=tail;
tail->prev=head;
Maxsize=capacity;// Set maximum
}
int get(int key) {
if(num.find(key)==num.end()){
// If there is no such keyword , The output -1
return -1;
}
Link *p;// Insert the used node into the head of the linked list
p=num[key];
p->prev->next=p->next;
p->next->prev=p->prev;
Link *temp=head->next;
head->next=p;
p->prev=head;
p->next=temp;
temp->prev=p;
return p->v;
}
void put(int key, int value) {
if(num.find(key)!=num.end()){// If map The keyword already exists in , Then change value value
num[key]->v=value;
Link *p;
p=num[key];// Move the node to the front
p->prev->next=p->next;
p->next->prev=p->prev;
Link *temp=head->next;
head->next=p;
p->prev=head;
p->next=temp;
temp->prev=p;
return;
}
size++;// Length plus one
Link *p;
p=new Link();// Join this node , Add the header
p->k=key;
p->v=value;
p->next=head->next;
p->next->prev=p;
head->next=p;
p->prev=head;
num[key]=p;
if(size>Maxsize){// If the maximum capacity is exceeded , Then remove the earliest used node , That is, the node at the end of the table
int k=tail->prev->k;
tail->prev=tail->prev->prev;
tail->prev->next=tail;
num.erase(k);// from map Remove the node from the table
}
}
};
This problem is mainly the combination of hash table and bidirectional linked list , The hash table key The value corresponds to the node of the bidirectional linked list ,
Follow the nearest node to the header , The earlier you use, the farther away from the end of the table , When the capacity exceeds the maximum limit ,
Remove the nodes at the end of the table , At the same time, don't forget to remove the values in the hash table .
边栏推荐
- 5G电视难成竞争优势,视频资源成中国广电最后武器
- LVGL 8.2 Draw label with gradient color
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
- Helix Swarm中文包发布,Perforce进一步提升中国用户体验
- Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
- 深度学习 神经网络案例(手写数字识别)
- UFO:微软学者提出视觉语言表征学习的统一Transformer,在多个多模态任务上达到SOTA性能!...
- 【学习笔记】拟阵
- Numpy notes
- C language set operation
猜你喜欢

Guitar Pro 8win10最新版吉他学习 / 打谱 / 创作

Ali was laid off employees, looking for a job n day, headhunters came bad news

近一亿美元失窃,Horizon跨链桥被攻击事件分析

03-存储系统

LVGL 8.2 LED

How to handle exceptions in multithreading?

暑期复习,一定要避免踩这些坑!

如何配和弦

How to match chords

Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
随机推荐
They are all talking about Devops. Do you really understand it?
Luo Gu - some interesting questions
5G电视难成竞争优势,视频资源成中国广电最后武器
Partial modification - progressive development
自动控制原理快速入门+理解
IO流:节点流和处理流详细归纳。
暑期复习,一定要避免踩这些坑!
C language book rental management system
宽度精度
十六进制
openresty 重定向
ES6 modularization
进制乱炖
夜天之书 #53 Apache 开源社群的“石头汤”
LVGL 8.2 LED
Introduction to asynchronous task capability of function calculation - task trigger de duplication
leecode学习笔记-约瑟夫问题
Wt588f02b-8s (c006_03) single chip voice IC scheme enables smart doorbell design to reduce cost and increase efficiency
LVGL 8.2 Draw label with gradient color
Redis 發布和訂閱