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 ...
随机推荐
- hdu 1047 (big integer sum, fgets or scanf, make you func return useful infos) 分类: hdoj 2015-06-18 08:21 39人阅读 评论(0) 收藏
errors made, boundary conditions, <= vs < , decreasing vs increasing , ++, –, '0'/'1' vs 0/1 p ...
- 正则表达式 java
如果你曾经用过Perl或任何其他内建正则表达式支持的语言,你一定知道用正则表达式处理文本和匹配模式是多么简单.如果你不熟悉这个术语,那么"正则表达式"(Regular Expres ...
- Tri-Training: Exploiting Unlabeled Data Using Three Classifiers
Abstract – In many practical data mining applications such as web page classification, unlabeled tra ...
- iOS 服务器端推送证书p12文件制作
A.苹果服务器地址: Production和development用的push的服务器不同pdev是:$apnsHost = 'gateway.sandbox.push.apple.com';pro是 ...
- github的注册过程
带着疑问打开了github.这是一个神奇的网站,因为它到处都是英语,对于我这种英语盲这简直太痛苦了.借助了百度翻译,我还是马马虎虎的完成了github的制作. 首先在它的登录界面下面有一个sign u ...
- checkbox选中与取消选择
先上代码 <form> 你爱好的运动是?<br/> <input type="checkbox" name="items" val ...
- mysql插入数据后返回自增ID的方法
mysql和oracle插入的时候有一个很大的区别是,oracle支持序列做id,mysql本身有一个列可以做自增长字段,mysql在插入一条数据后,如何能获得到这个自增id的值呢? 方法一是使用la ...
- ios之AFN上传下载详细步骤(2)
五.AFN .GET\POST > GET请求 // 1.获得请求管理者 AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperation ...
- SQL Analysis Services MDX 查询超时 解决办法
当页面有很多MDX语句查询的时候,会发生超时的情况. 解决办法: SQL Analysis Services所在的服务器(OLAP的文件夹下) 找到: msmdpump.ini 将: <Conf ...
- KVC/KVO原理详解及编程指南
一.简介 1.KVC简介 2.KVO简介 二.KVC相关技术 1.Key和Key Path 2.点语法和KVC 3.一对多关系(To-Many)中的集合访问器方法 4.键值验证(Key-Value V ...