首页 > 分享 > 蓝桥11

蓝桥11

一LRU缓存机制

在这里插入图片描述

创建两个量,记录最大容量,记录缓存信息获取值:如果存在,先将当前值pop出,然后再插入尾部,如果没有直接返回-1存值:如果已经存在,则直接取出,然后插入到尾部,如果不存在直接插入尾部存值时需要判定当前缓存是否超出最大限制,如果超出限制,则需要移除最前面的

class LRUCache: def __init__(self, capacity: int): self.capacity = capacity self.caches = dict() def get(self, key: int) -> int: # 获取一个元素,那么这个元素就要从字典中取出,然后将其移动到最后去 if key in self.caches: val = self.caches[key] self.caches.pop(key) self.caches[key] = val return self.caches.get(key,-1) def put(self, key: int, value: int) -> None: # 如果值已经存在则移除 if key in self.caches: self.caches.pop(key) self.caches[key] = value if len(self.caches) > self.capacity: first = [*self.caches.keys()][0] self.caches.pop(first) # Your LRUCache object will be instantiated and called as such: # obj = LRUCache(capacity) # param_1 = obj.get(key) # obj.put(key,value)

123456789101112131415161718192021222324252627282930

二排序链表

在这里插入图片描述

先记录链表的长度,创建一个数组,将链表中的数全部储存到数组中,然后通过数组Sort排序,最后用链表输出

/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int val=0, ListNode next=null) { * this.val = val; * this.next = next; * } * } */ public class Solution { public ListNode SortList(ListNode head) { int len=0; ListNode p=head; while(p!=null) { len++; p=p.next; } int[] array=new int[len]; p=head; for(int i=0;i<len;i++) { array[i]=p.val; p=p.next; } Array.Sort(array); p=head; for(int i=0;i<len;i++) { p.val=array[i]; p=p.next; } return head; } }

123456789101112131415161718192021222324252627282930313233343536373839

三最小栈

在这里插入图片描述

定义两个空列表来充当栈结构,一个存放真正栈中的数据,一个存放栈中最小值的数据,在第一次push的时候,不管这个值是多少,都要放到存放最小值这个列表里面,在之后push值得时候,比较,当前插入得值是不是比,minstack列表中最后一个值小,小得话就插入,当删除栈中得值时,也需要和minstack中最后一个值比较,因为这样会节省内存,将不必要得值删除

class MinStack: def __init__(self): """ initialize your data structure here. """ self.item = [] self.minstack = [] def push(self, x: int) -> None: self.item.append(x) if not self.minstack or self.minstack[-1] >= x: self.minstack.append(x) def pop(self) -> None: if self.item.pop() == self.minstack[-1]: self.minstack.pop() def top(self) -> int: return self.item[-1] def getMin(self) -> int: return self.minstack[-1] # Your MinStack object will be instantiated and called as such: # obj = MinStack() # obj.push(x) # obj.pop() # param_3 = obj.top() # param_4 = obj.getMin()

12345678910111213141516171819202122232425262728293031323334

相关知识

蓝桥杯大赛——视觉艺术设计赛
蓝桥杯 算法训练 字符串编辑
第八届蓝桥杯全国总决赛真题解析
蓝桥杯攻略!省一经验+考试全流程+技巧分享
奶包四岁半,十个哥哥团宠我最新章节,醉蓝桥,奶包四岁半,十个哥哥团宠我全文阅读
“魔力鸟”誓言再展切尔西雄风 将交给时间检验
2024年,ICPC比赛、CCPC比赛、CCF
我整理了中小学生白名单竞赛的报名时间表,24年大家别错过!
【重磅收藏】2022年中小学科技类竞赛时间表!2022年赛事如何安排——上篇
收藏!2021各类编程赛事时间表盘点

网址: 蓝桥11 https://m.mcbbbk.com/newsview410843.html

所属分类:萌宠日常
上一篇: 送宠物去培训一个月多少钱
下一篇: 奥运会小知识:夏奥、残奥、冬奥,