当前位置:网站首页>力扣刷题01(反转链表+滑动窗口+LRU缓存机制)
力扣刷题01(反转链表+滑动窗口+LRU缓存机制)
2022-07-04 13:32:00 【sun*san】
好久没写总结的说,然后前段时间都忙着在写项目,刷题很少刷,那今天就来写一篇题解吧。
反转链表
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* p;
p=head;
ListNode* q=nullptr;
while(p){
ListNode* next = p->next;//记录当前位置的下一位
p->next=q;//将当前位置的链条与上一位链接
q=p;//更新上一位
p=next;//移到下一个位置继续之前的操作
}
return q;
}
};
滑动窗口
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if(s==""){//特判无字符的情况
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()){//如果有相同字符时,更新i下标
i=max(a[s[j]],i);
}
sum=max(sum,j-i+1);//更新sum值
a[s[j]]=j+1;//将value值设为该字符下标+1,使易获取字符位置
}
return sum;
}
};一开始用的暴力,果然不对,时间超限,之后用到了窗口滑动,通过更新i,j两个下标来寻找长度最大值

struct Link{//创建双向链表结点
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();//双向链表初始化
tail=new Link();
head->next=tail;
tail->prev=head;
Maxsize=capacity;//设置最大值
}
int get(int key) {
if(num.find(key)==num.end()){
//如果没有该关键字,则输出-1
return -1;
}
Link *p;//将该使用到的结点插入链表头部
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()){//如果map中已有该关键字,则改动value值
num[key]->v=value;
Link *p;
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;
}
size++;//长度加一
Link *p;
p=new Link();//加入该结点,加入表头
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){//如超出最大容量,则移除最早使用过的结点,即表尾的结点
int k=tail->prev->k;
tail->prev=tail->prev->prev;
tail->prev->next=tail;
num.erase(k);//从map表中移除该结点
}
}
};
该题主要是哈希表和双向链表的结合使用,哈希表的key值对应的是双向链表的结点,
遵循最近使用的结点离表头越近,越早使用的离表尾越远的原则,当容量超出最大限制时,
去除表尾的结点,同时别忘记去除哈希表中的值。
边栏推荐
- [information retrieval] link analysis
- Redis 发布和订阅
- LVLG 8.2 circular scrolling animation of a label
- C language personal address book management system
- Query optimizer for SQL optimization
- Free, easy-to-use, powerful lightweight note taking software evaluation: drafts, apple memo, flomo, keep, flowus, agenda, sidenote, workflow
- Implementation of macro instruction of first-order RC low-pass filter in signal processing (easy touch screen)
- openresty 限流
- The performance of major mainstream programming languages is PK, and the results are unexpected
- Yyds dry goods inventory # solve the real problem of famous enterprises: continuous maximum sum
猜你喜欢

信号处理之一阶RC低通滤波器宏指令实现(繁易触摸屏)

LVGL 8.2 Line

Techsmith Camtasia Studio 2022.0.2屏幕录制软件

对话龙智高级咨询顾问、Atlassian认证专家叶燕秀:Atlassian产品进入后Server时代,中国用户应当何去何从?

Details of FPGA underlying resources
![[information retrieval] experiment of classification and clustering](/img/05/ee3b3bc4ab79d52b63cdc34305aa57.png)
[information retrieval] experiment of classification and clustering

LVGL 8.2 LED

When synchronized encounters this thing, there is a big hole, pay attention!
![[differential privacy and data adaptability] differential privacy code implementation series (XIV)](/img/de/c053f376fe90c2697ffc640fab57e8.jpg)
[differential privacy and data adaptability] differential privacy code implementation series (XIV)

阿里被裁员工,找工作第N天,猎头又传来噩耗...
随机推荐
Leecode learning notes - Joseph problem
自动控制原理快速入门+理解
Helix Swarm中文包发布,Perforce进一步提升中国用户体验
Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist
[C language] Pointer written test questions
Explain of SQL optimization
(1) The standard of performance tuning and the correct posture for tuning - if you have performance problems, go to the heapdump performance community!
LVGL 8.2 List
Ffprobe common commands
Ali was laid off employees, looking for a job n day, headhunters came bad news
如何配和弦
Is it safe to open an account online for stock speculation? Will you be cheated.
LVGL 8.2 Draw label with gradient color
LVGL 8.2 text shadow
Helix swarm Chinese package is released, and perforce further improves the user experience in China
Techsmith Camtasia Studio 2022.0.2屏幕录制软件
Leetcode 1200 minimum absolute difference [sort] The Path of leetcode for heroding
智能客服赛道:网易七鱼、微洱科技打法迥异
Redis publier et s'abonner
毕业季-个人总结