432. All O`one Data Structure (类似 LFU算法的设计)
来源:互联网 发布:手机怎样修改淘宝密码 编辑:程序博客网 时间:2024/06/18 00:54
题目地址
https://leetcode.com/problems/all-oone-data-structure/?tab=Description
ac转载
http://www.cnblogs.com/grandyang/p/6012229.html
主要是双向链表 + map结构
数据结构设计
下面是纯粹用自己写的双向链表 和 C++ map 的ac代码
需要注意各种inc和dec处理的各种情况
typedef struct node{ int val; set<string> keys; struct node* prev; struct node* next;}Dnode;class AllOne {public: Dnode* head;// 双向链表,头部存最大的,尾部存最小的 Dnode* tail; map<string,Dnode*> m; // mappublic: AllOne() { head = new Dnode; tail = new Dnode; head->next = tail; head->prev = NULL; tail->prev = head; tail->next = NULL; } void inc(string key) { if(m.count(key) == 0) // 不存在 { // 新插入的val就等于1,需要判断双向链表是否有val=1的 if(tail->prev == head || tail->prev->val != 1) { Dnode* dn = new Dnode; dn->val = 1; dn->keys.insert(key); Dnode* tailpre = tail->prev; tail->prev = dn; dn->next = tail; tailpre->next = dn; dn->prev = tailpre; m[key] = dn; }else{ tail->prev->keys.insert(key); m[key] = tail->prev; } }else{ // 存在,key所处的val需要加上1 Dnode* tmp = m[key]; Dnode* lastNode = tmp->prev; if(lastNode == head || lastNode->val != tmp->val + 1) { Dnode* newDn = new Dnode; newDn->val = tmp->val + 1; newDn->keys.insert(key); lastNode->next->prev = newDn; newDn->next = lastNode->next; newDn->prev = lastNode; lastNode->next = newDn; m[key] = newDn; }else{ lastNode->keys.insert(key); m[key] = lastNode; } tmp->keys.erase(key); if(tmp->keys.empty()) { Dnode* tmpPre = tmp->prev; tmp->next->prev = tmpPre; tmpPre->next = tmp->next; delete tmp; } } } void dec(string key) { if(m.count(key) == 0) return; Dnode* curDn = m[key]; if(curDn->val == 1) { curDn->keys.erase(key); if(curDn->keys.empty()) { Dnode* tmpPre = curDn->prev; curDn->next->prev = tmpPre; tmpPre->next = curDn->next; delete curDn; m.erase(key); return; } } Dnode* nextDn = curDn->next; if(nextDn == tail || nextDn->val != curDn->val -1) { if(curDn->val - 1 == 0) { m.erase(key); }else{ Dnode* newDn = new Dnode; newDn->val = curDn->val - 1; newDn->keys.insert(key); curDn->next->prev = newDn; newDn->next = curDn->next; newDn->prev = curDn; curDn->next = newDn; m[key] = newDn; } }else{ nextDn->keys.insert(key); m[key] = nextDn; } curDn->keys.erase(key); if(curDn->keys.empty()) { Dnode* tmpPre = curDn->prev; curDn->next->prev = tmpPre; tmpPre->next = curDn->next; delete curDn; } } string getMaxKey() { if(head->next == tail) return ""; return *(head->next->keys.begin()); } string getMinKey() { if(head->next == tail) return ""; return *(tail->prev->keys.begin()); }};
0 0
- 432. All O`one Data Structure (类似 LFU算法的设计)
- leetcode 432. All O`one Data Structure
- 432. All O`one Data Structure
- 432. All O`one Data Structure
- LeetCode 432. All O`one Data Structure
- [LeetCode]432. All O`one Data Structure
- leetcode 432. All O`one Data Structure
- 432. All O`one Data Structure
- leetcode 432. All O`one Data Structure
- All O`one Data Structure
- [Leetcode] 432. All O`one Data Structure 解题报告
- [LeetCode] All O`one Data Structure
- Leetcode 432. All O`one Data Structure 高效的数据结构 解题报告
- 复杂度为O(1)的最不常用[LFU]缓存算法
- 460. LFU Cache O(1)算法 C++
- 页面置换算法--LFU算法实现-O(1)时间复杂度
- 【Data Structure】RMQ问题的Sparse-Table算法
- LFU算法
- 算法竞赛入门经典ch1_ex_鸡兔同笼
- c语言中:结构体的内存分配
- HTML5基本元素的使用
- 欢迎使用CSDN-markdown编辑器
- 程序的路径
- 432. All O`one Data Structure (类似 LFU算法的设计)
- YII2.0学习笔记2-初步了解运行原理
- 串口文件_写
- 在WIN10下配置JAVA编辑和运行的环境
- Bossies 2016:最佳开源大数据工具
- setiosflags和数制基数的用法
- 磁铁的使用 —— 生活中的实验
- Tomorrow never knows? (C程序设计进阶 第2周)
- 56681572