一、LRU缓存
链接:https://leetcode-cn.com/problems/lru-cache
题目描述:
请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。
实现 LRUCache 类:
LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存
int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。
void put(int key, int value) 如果关键字 key 已经存在,则变更其数据值 value ;如果不存在,则向缓存中插入该组 key-value 。如果插入操作导致关键字数量超过 capacity ,则应该 逐出 最久未使用的关键字。
函数 get 和 put 必须以 O(1) 的平均时间复杂度运行。
示例:
输入
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
输出
[null, null, null, 1, null, -1, null, -1, 3, 4]
解释
LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // 缓存是 {1=1}
lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
lRUCache.get(1); // 返回 1
lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3}
lRUCache.get(2); // 返回 -1 (未找到)
lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}
lRUCache.get(1); // 返回 -1 (未找到)
lRUCache.get(3); // 返回 3
lRUCache.get(4); // 返回 4
完整代码:
/*思路:建立一个双向链表,类型为pair<int,int>,存储键值和值
再建立一个unorederor_map<int,iterator>,存储键值和链表的迭代器。
LRU(最近最少使用),所以每次get和put操作都将该数据移动到链表的头部,这样保证了链表最后面数据为最少使用的。
*/
class LRUCache {
public:
LRUCache(int capacity) : size(capacity){
}
int get(int key) {
auto iter = hash.find(key);
if(iter != hash.end())
{
//将该数据向链表头移动
ls.splice(ls.begin(),ls,iter->second);
return iter->second->second;
}
return -1;
}
void put(int key, int value) {
auto iter = hash.find(key);
if(iter != hash.end())
{
//存在则将数据向链表头移动
ls.splice(ls.begin(),ls,iter->second);
iter->second->second = value;
return;
}
ls.emplace_front(key,value);
hash[key] = ls.begin();
if(size < hash.size())
{
hash.erase(ls.back().first);
ls.pop_back();
}
}
private:
unordered_map<int, std::list<std::pair<int,int>>::iterator> hash;
std::list<std::pair<int,int>> ls;
int size;
};
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache* obj = new LRUCache(capacity);
* int param_1 = obj->get(key);
* obj->put(key,value);
*/
二、反转链表
链接:https://leetcode-cn.com/problems/reverse-linked-list/
题目描述:
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
1->2->3->4->5
变为
5->4->3->2->1
完整代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head == nullptr) return head;
ListNode* last = nullptr;
ListNode* pre = head;
while(pre)
{
ListNode* tmep = pre->next;
pre->next = last;
last = pre;
pre = tmep;
}
return last;
}
};
三、排序数组(手撕快速排序)
链接:https://leetcode-cn.com/problems/sort-an-array
题目描述:
给你一个整数数组 nums,请你将该数组升序排列。
示例:
输入:nums = [5,2,3,1]
输出:[1,2,3,5]
完整代码:
class Solution {
public:
void quickSort(vector<int>& nums,int left,int right)
{
if(right < left) return ;
int key = qSort(nums,left,right);
quickSort(nums,left,key-1);
quickSort(nums,key+1,right);
}
int qSort(vector<int>& nums,int left,int right)
{
//防止数组为已经有序
swap(nums[left], nums[(left + right) / 2]);
/*
//随机取基数,也能避免数组有序
int x = rand() % (right - left + 1) + left;
swap(nums[left],nums[x]);
*/
int key = nums[left];
while(left < right)
{
while(right > left && nums[right] >= key)
right--;
nums[left] = nums[right];
while(left < right && nums[left] <= key)
left++;
nums[right] = nums[left];
}
nums[left] = key;
return left;
}
vector<int> sortArray(vector<int>& nums) {
quickSort(nums,0,nums.size()-1);
return nums;
}
};