当前位置:网站首页>力扣刷题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值对应的是双向链表的结点,
遵循最近使用的结点离表头越近,越早使用的离表尾越远的原则,当容量超出最大限制时,
去除表尾的结点,同时别忘记去除哈希表中的值。
边栏推荐
- Luo Gu - some interesting questions
- Docker compose public network deployment redis sentinel mode
- 对话龙智高级咨询顾问、Atlassian认证专家叶燕秀:Atlassian产品进入后Server时代,中国用户应当何去何从?
- 智能客服赛道:网易七鱼、微洱科技打法迥异
- Expose Ali's salary and position level
- UFO: Microsoft scholars have proposed a unified transformer for visual language representation learning to achieve SOTA performance on multiple multimodal tasks
- 5g TV cannot become a competitive advantage, and video resources become the last weapon of China's Radio and television
- Details of FPGA underlying resources
- A collection of classic papers on convolutional neural networks (deep learning classification)
- Dialogue with ye Yanxiu, senior consultant of Longzhi and atlassian certification expert: where should Chinese users go when atlassian products enter the post server era?
猜你喜欢
Luo Gu - some interesting questions
《opencv学习笔记》-- 线性滤波:方框滤波、均值滤波、高斯滤波
Kubernets pod exists finalizers are always in terminating state
Ultrasonic distance meter based on 51 single chip microcomputer
Five minutes per day machine learning: use gradient descent to complete the fitting of multi feature linear regression model
92. (cesium chapter) cesium building layering
03 storage system
UFO: Microsoft scholars have proposed a unified transformer for visual language representation learning to achieve SOTA performance on multiple multimodal tasks
Transplant tinyplay for imx6q development board QT system
音视频技术开发周刊 | 252
随机推荐
LeetCode 1200 最小絕對差[排序] HERODING的LeetCode之路
对话龙智高级咨询顾问、Atlassian认证专家叶燕秀:Atlassian产品进入后Server时代,中国用户应当何去何从?
C language set operation
Helix Swarm中文包发布,Perforce进一步提升中国用户体验
PLC Analog input analog conversion FC s_ ITR (CoDeSys platform)
韩国AI团队抄袭震动学界!1个导师带51个学生,还是抄袭惯犯
LVGL 8.2 Draw label with gradient color
C language personal address book management system
Weibo and Huya advance into interest communities: different paths for peers
Ffmpeg Visual Studio development (IV): audio decoding
浮点数如何与0进行比较?
LVGL 8.2 Line wrap, recoloring and scrolling
Techsmith Camtasia Studio 2022.0.2屏幕录制软件
Xcode abnormal pictures cause IPA packet size problems
找数字
5g TV cannot become a competitive advantage, and video resources become the last weapon of China's Radio and television
What are the concepts of union, intersection, difference and complement?
LVGL 8.2 keyboard
C language achievement management system for middle school students
Luo Gu - some interesting questions 2