43. Merge Sorted Array && LRU Cache
Merge Sorted Array
OJ: https://oj.leetcode.com/problems/merge-sorted-array/
Given two sorted integer arrays A and B, merge B into A as one sorted array.
Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.
思想:因为 A 很大, 所以从最大值开始插入, 即从 A 的 m+n 位置开始插入数据。避免了冗余的移动。
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int end = m+n-1;
int iA = m-1, iB = n-1;
while(iB >= 0) {
if(iA < 0 || A[iA] <= B[iB]) A[end--] = B[iB--];
else A[end--] = A[iA--];
}
}
};
LRU Cache
OJ: https://oj.leetcode.com/problems/lru-cache/
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get
and set
.
get(key)
- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. set(key, value)
- Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
思想:
1. 由于要 O(1) 时间确定某 key 是不是在 Cache 中,所以用 Hash_map (<key, node*>), 从而能够O(1)找到结点地址,返回对应的 value。
2. 由于要 O(1) 时间插入、删除某项, 所以各项之间的存储不能用单链表(删除时要O(n)查找前面的结点),不能用顺序表(插入、删除都 O(n)), 故存储使用双链表。
综上分析,查找、插入、删除都是 O(1)时间。(代码尚可优化)
typedef struct node {
node *pre, *next;
int key;
int value;
node() : pre(NULL), next(NULL), key(0), value(0) {}
node(int k, int v) : pre(NULL), next(NULL), key(k), value(v) {}
} DoubleLinkList; class LRUCache{
public:
LRUCache(int capacity) : _capacity(capacity), cnt(0), front(NULL), tail(NULL) {} int get(int key) {
unordered_map<int, node*>::iterator it = _map.find(key);
if(it == _map.end()) return -1; node* s = it->second;
if(s != front) {
if(s == tail) {
tail = s->pre;
s->pre = NULL;
tail->next = NULL;
front->pre = s;
s->next = front;
front = s;
}else {
s->pre->next = s->next;
s->next->pre = s->pre;
s->next = front;
front->pre = s;
front = s;
}
}
return it->second->value;
}
void set(int key, int value) {
unordered_map<int, node*>::iterator it = _map.find(key);
if(it == _map.end()) {
if(++cnt > _capacity) {
if(front == tail) {
_map.erase(tail->key);
front = tail = NULL; }else{
node *s = tail;
tail = tail->pre;
tail->next = NULL;
_map.erase(s->key);
free(s);
--cnt;
}
}
node *p = new node(key, value);
if(front == NULL) {
front = tail = p;
}else {
p->next = front;
front->pre = p;
front = p;
}
_map.insert(pair<int, node*>(key, p));
}else {
it->second->value = value;
node *s = it->second;
if(s == front) {
return;
}else if(s == tail) {
tail = s->pre;
s->pre = NULL;
tail->next = NULL;
s->next = front;
front->pre = s;
front = s;
}else {
s->pre->next = s->next;
s->next->pre = s->pre;
s->pre = NULL;
s->next = front;
front->pre = s;
front = s;
}
}
}
unordered_map<int, node*> _map;
DoubleLinkList *front, *tail;
int _capacity;
int cnt;
};
43. Merge Sorted Array && LRU Cache的更多相关文章
- [LeetCode] Merge Sorted Array 混合插入有序数组
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...
- 【LeetCode练习题】Merge Sorted Array
Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...
- Leetcode#88. Merge Sorted Array(合并两个有序数组)
题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...
- 【LeetCode】88. Merge Sorted Array (2 solutions)
Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- [LeetCode] 88. Merge Sorted Array 混合插入有序数组
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- LeetCode_88. Merge Sorted Array
88. Merge Sorted Array Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- LeetCode练题——88. Merge Sorted Array
1.题目 88. Merge Sorted Array——Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into ...
- Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
随机推荐
- 手机抓包-fiddler
如果app走的是http协议,不用root,只需要通过fiddler做代理,就可以抓到所有请求. 1. fiddler+手机wifi设置 安装fiddler,勾中 Fiddler Options -& ...
- 百度定位并获取县区天气-XML+fragment+sqlite
此工程较BaiduLocationXMLFragment相比:1.加入数据库部分,将获取到的地址 天气存入数据库中,离线状态显示数据库最后一条记录 sqlite: DatabaseHelper.ja ...
- 谷歌浏览器中安装.crx扩展名的离线Chrome插件
一.本地拖放安装 1.下载扩展程序/脚本程序至本地计算机: 2.将其直接拖拽到浏览器的“扩展程序”(chrome://chrome/extensions/)页面. 二.解决“只能通过Chrome网上应 ...
- webshell
webshell就是以asp.php.jsp或者cgi等网页文件形式存在的一种命令执行环境,也可以将其称做为一种网页后门.黑客在入侵了一个网站后,通常会将asp或php后门文件与网站服务器WEB目录下 ...
- js---html---body标签
<body bgcolor="背景颜色" background="背景图片" text="文本颜色" link="连接文件颜 ...
- CI整合Smarty
1.到相应的站点下载smarty模板: 2.将源代码中的libs目录复制到项目的libraries目录下,改名为smarty3.0 3.在项目目录的libraries文件夹内新建文件ci_smarty ...
- 避免每次输入bundler Exec命令
bundle在ruby的世界里是个好东西,它可以用来管理应用程序的依赖库.它能自动的下载和安装指定的gem,也可以随时更新指定的gem. rvm则是一个命令行工具,能帮助你轻松的安装,管理多个ruby ...
- ubuntu 14.04 LTS 安装ss客户端
附: 配置pac模式 ss客户端 ss客户端 前提环境 python (用最新的就行) pip (注:python工具) build-essential 以上可以通过一条命令解决: sudo apt- ...
- ArrayBlockingQueue-我们到底能走多远系列(42)
我们到底能走多远系列(42) 扯淡: 乘着有空,读些juc的源码学习下.后续把juc大致走一边,反正以后肯定要再来. 主题: BlockingQueue 是什么 A java.util.Queue t ...
- PCL常见编程问题
1.如何获取pcd文件点云里点的格式,比如是pcl::PointXYZ还是pcl::PointXYZRGB等类型? #include <pcl/io/pcd_io.h> #include ...