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 ...
随机推荐
- ionic 安装遇到的问题以及解决方案
公司里要用到 Ionic 做移动App 混合开发 一个环境搭建折腾了好几天.一是公司权限问题,二是网络问题,你懂得. Ionic 环境搭建官网有教程.本来几行命令就能搞定的事,一旦遇到网络问题,就蛋疼 ...
- ExtJs4中的复选树级联选择
好久没有写新的博文了,过了个年休息了近一个月,人都懒散了.. 这几天要把项目中的几个模块有ext3升级到ext4,还要保持页面展示和功能要跟3.x版本的一样.升级并不是一件简单的是,基本相当于重写了, ...
- 由ArrayList构造函数源码引出的问题
ArrayList应该用得很多了.最近看了看其源码,发现有很多细节,如果要我们自己来实现,估计会考虑不到.当然,这些细节跟jdk本身一些实现的bug有关,如果不去深挖,定然是不能发现.本文从Array ...
- Ubuntu下快速安装LAMP server
Ubuntu下可快速安装LAMP server(Apache+MySQL+PHP5). 首先,打开Ubuntu虚拟机,Terminal打开root权限:“sudo -s”. 一.安装LAMP serv ...
- <Java中的继承和组合之间的联系和区别>
//Java中的继承和组合之间的联系和区别 //本例是继承 class Animal { private void beat() { System.out.println("心胀跳动...& ...
- Android中build target,minSdkVersion,targetSdkVersion,maxSdkVersion概念区分 (转载)
本文参考了谷歌开发者文档:http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#provisional 如果 ...
- 《C++primer》v5 第1章 开始 读书笔记 习题答案
从今天开始在博客里写C++primer的文字.主要以后面的习题作业为主,会有必要的知识点补充. 本人也是菜鸟,可能有不对之处,还望指出. 前期内容可能会比较水. 1.1略 1.2略 1.3 cin和c ...
- 同域名下PC与移动端自动识别跳转
输入相同域名,在pc端和移动端会出现不同的页面效果,一种是用栅格系统实现自适应,更多的是设计两套不同的模板和两个二级域名或者一个主域名和一个二级域名(就是有区别就可以了); js代码判断浏览器的用户代 ...
- BlackJack Strategy
GAME SPEC: 2-deck, 104 cards total. Bellagio has 2-deck and 6-deck games. based on hard 17, dealer h ...
- css模糊效果
CSS代码: .blur { filter: url(blur.svg#blur); /* FireFox, Chrome, Opera */ -webkit-filter: blur(10px); ...