classLRUCache:def__init__(self, capacity:int):
self.capacity = capacity
self.caches =dict()defget(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)defput(self, key:int, value:int)->None:# 如果值已经存在则移除if key in self.caches:
self.caches.pop(key)
self.caches[key]= value
iflen(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)
二排序链表
先记录链表的长度,
创建一个数组,
将链表中的数全部储存到数组中,
然后通过数组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;}}
classMinStack:def__init__(self):"""
initialize your data structure here.
"""
self.item =[]
self.minstack =[]defpush(self, x:int)->None:
self.item.append(x)ifnot self.minstack or self.minstack[-1]>= x:
self.minstack.append(x)defpop(self)->None:if self.item.pop()== self.minstack[-1]:
self.minstack.pop()deftop(self)->int:return self.item[-1]defgetMin(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()