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 ...
随机推荐
- sprite图在移动端的使用
做移动端页面时,设计稿中的切片图片往往是实际的2倍,此处记录图片正常显示大小的技巧. 当图片是单张的话,可以对容器设计实际大小,然后设置background-image,为了让图片缩小一倍,可以设置b ...
- 去掉安卓中activity的标题栏
去掉所有Activity界面的标题栏 修改AndroidManifest.xml 在application 标签中添加android:theme="@android:style/Theme. ...
- 字符串中带有emoji表情处理
1:先删除字符然后解析当前字符再显示 edit.addTextChangedListener(new TextWatcher() { @Override public void beforeTextC ...
- IIS 工作原理之非托管代码旅程(一)
IIS6改变 IIS6可以为每个不同的虚拟目录创建不同的程序池,这样缩小了IIS的Application Pool的颗粒度,不同的虚拟目录之间是互不影响的. IIS6(和IIS7经典模式)与IIS7集 ...
- MagicalRecord 多表关联数据操作
最近在使用MagicalRecord做数据持久层CoreData的操作库,今天做了一个多表关联数据的操作,整理了一个demo,特此记录一下. 关于如何使用Cocopads 和 MagicalRecor ...
- 黑马程序员:Java编程_String
=========== ASP.Net+Android+IOS开发..Net培训.期待与您交流!=========== 描述字符串对象的类是java.lang.String,String类是不可变(f ...
- .Net内存泄露原因及解决办法
.Net内存泄露原因及解决办法 1. 什么是.Net内存泄露 (1).NET 应用程序中的内存 您大概已经知道,.NET 应用程序中要使用多种类型的内存,包括:堆栈.非托管堆和托管堆.这里我们需 ...
- 今天<人人都能弹吉他>免费版获得了苹果的新品推荐
今天改了一天程序, 来回测来回改, 准备提交一个新版本了. 傍晚跑步回来, 看了一下今天的下载量, 竟然比昨天多了. 然后就想, 难不成被苹果推荐了? 上线一看, 果然, 而且美国和中国两大市场都在新 ...
- PHP 调用Python脚本
上次做用户反馈自动翻译,写了个python脚本,将日文的用户反馈翻译成中文,效果虽然可以,但其它不懂python的童鞋就没法使用了,所以搭了个web服务,让其他人可以通过网页访问查询.使用的是apac ...
- JavaSE坦克网络版
02.1.建立Server(保持这个TankServer一直运行) package server; public class TankServer { public static void main( ...