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的更多相关文章

  1. [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 ...

  2. 【LeetCode练习题】Merge Sorted Array

    Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...

  3. Leetcode#88. Merge Sorted Array(合并两个有序数组)

    题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...

  4. 【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 ...

  5. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

  6. [LeetCode] 88. Merge Sorted Array 混合插入有序数组

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  7. LeetCode_88. Merge Sorted Array

    88. Merge Sorted Array Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1  ...

  8. LeetCode练题——88. Merge Sorted Array

    1.题目 88. Merge Sorted Array——Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into  ...

  9. Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

随机推荐

  1. 服务器端与客户端TCP连接入门(二)

    1.服务器端程序 package Socket; import java.io.BufferedReader; import java.io.IOException; import java.io.I ...

  2. js 判断数据类型的方法及实现

    转载自 http://blog.csdn.net/xujiaxuliang/archive/2009/10/21/4708353.aspx null 与 undefined 区别: null 是js的 ...

  3. 在 Vultr VPS 中 以 Debian 8 i386 (jessie) 为 操作系统 平台 手动 搭建 PPTP VPN 全过程

    更新服务器并安装 PPTP 服务  apt-get update apt-get upgrade apt-get install pptpd 编辑 /etc/pptpd.conf 找到 #locali ...

  4. CENTOS/UBUNTU一键安装IPSEC/IKEV2 VPN服务器

    1.在azure上创建ubuntu虚拟机 选择v15.04 server 版本 2.添加端口号 3.远程桌面到ubuntu 命令行 输入 sudo su  输入创建 ubuntu虚拟机 时候的 密码 ...

  5. 解决iOS8安装企业版无反应问题

    iOS7可以下载没有任何问题,iOS8发现挂在官网上的企业版的app点击了提示是否安装应用程序,但是确认以后没有反应,找了很久,都没有发现问题.后来查看了的device console发现安装的时候出 ...

  6. This tag and its children can be replaced by one <TextView/> and a compound drawable

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  7. LVDS,MIPI,EDP

    一.背景介绍: 随着显示分辨率的越来越高,传统的VGA.DVI等接口逐渐不能满足人们的视觉需求.随后就产生了以HDMI.DisplayPort为代表的新型数字接口,外部接口方面HDMI占据了较大市场优 ...

  8. Python学习路程day2

    import sys      #接收执行参数 #!/usr/bin/env python import sys print (sys.argv)​ 例: >>>python ind ...

  9. iOS-服务器文件断点下载

    文件下载基本步骤:1.获取下载链接,创建响应发送请求.(使用异步请求,避免因文件过大下载时间长而阻塞主线程).2.当接到响应时在下载目录中创建文件.创建文件使用NSFileHandle进行文件内部处理 ...

  10. Codeforces Round #162 (Div. 2)

    A. Colorful Stones (Simplified Edition) 模拟. B. Roadside Trees (Simplified Edition) 每次转移时,只需要爬到\(min( ...