leetcode LRUCache题目
package com.jackiesteed.leetcode;import java.util.*;/*** Created by jackie on 4/23/15.*/public class LRUCache {private int capacity;private Map<Integer, Ele> map = new HashMap<Integer, Ele>();private Ele head = null;private Ele tail = null;public LRUCache(int capacity) {this.capacity = capacity;}public void set(int key, int value) {if(map.containsKey(key)){Ele e = map.get(key);if(head != e){unlink(e);add2Head(e);}e.value = value;return;}if(map.size() >= capacity){Ele e = tail;map.remove(e.key);e.value = value;e.key = key;map.put(key, e);unlink(e);add2Head(e);return;}Ele e = new Ele();e.key = key;e.value = value;e.pre = null;e.next = null;if(head == null || tail == null){head = e;tail = e;}else{add2Head(e);}map.put(key, e);}private void add2Head(Ele e){e.pre = null;e.next = head;head.pre = e;head = e;}public int get(int key) {if (map.containsKey(key)) {Ele e = map.get(key);if(head != e){unlink(e);add2Head(e);}return map.get(key).value;}return -1;}private void unlink(Ele e){if(e.pre != null)e.pre.next = e.next;if(e.next != null)e.next.pre = e.pre;if(tail == e)tail = e.pre;if(tail == null)tail = head;}static class Ele {private int value;private int key;private Ele next;private Ele pre;}public void print(){Ele e = head;while(true){if(e == null)break;System.out.print("(" + e.key + "," + e.value + "),");e = e.next;}System.out.println("");}public static void main(String[] args) {LRUCache cache = new LRUCache(3);cache.set(1,1);cache.set(2, 2);cache.set(3,3);cache.set(4,4);cache.print();cache.get(4);cache.get(3);cache.get(2);cache.get(1);cache.print();}}
转载于:https://www.cnblogs.com/jackiesteed/articles/4454906.html
leetcode LRUCache题目相关推荐
- 【持续更新】Leetcode SQL题目全解析(附建表sql)
Leetcode SQL题目全解析 越前须知(雾) 题目Q & A 175 组合两个表 181 超过经理收入的员工 182 查找重复电子邮箱 183 从不订购的用户 197 上升的温度 511 ...
- LeetCode数据库题目1-123
LeetCode数据库题目1-123 175. 组合两个表 难度简单 SQL架构 表1: Person +-------------+---------+ | 列名 | 类型 | +--------- ...
- 算法思想-深度搜索算法-leetcode相关题目总结
通过这篇文章你能学到什么 搜索算法 深度优先搜索 分析过程 实现代码 进出栈过程示意图 DFS算法应用-Leetcode相关题目 Leetcode 78 Subsets Leetcode 90 Sub ...
- LeetCode 所有题目总结
文章目录 做题注意事项 题目分类 1.位运算 2.字符串题型 3.TopK 问题--最大堆/最小堆 4.链表 5.动态规划 easy Medium hard 6.贪心 7.树 8.图 9.数学题 10 ...
- java 套娃_【leetcode编程题目354】俄罗斯套娃
来自 https://leetcode.com/problems/russian-doll-envelopes/ You have a number of envelopes with widths ...
- 【LeetCode刷题记录】LeetCode经典题目数组求和及哈希表的使用!
点上方蓝字计算机视觉联盟获取更多干货 在右上方 ··· 设为星标 ★,与你不见不散 LeetCode题目 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整 ...
- LeetCode简单题目(#263 #268 #278 #283 #290)-5道(数字、字符串)
leetcode题库中共有350道简单题目. 本文记录已解决的题目和代码. 本文中的序号是leetcode题目中的真实序号. 文章目录 263 丑数 描述 代码 大神代码 268 缺失数字 描述 代码 ...
- LeetCode简单题目(#235 #237 #242 #257 #258)-5道(树、数字、字符串)
leetcode题库中共有350道简单题目. 本文记录已解决的题目和代码. 本文中的序号是leetcode题目中的真实序号. 文章目录 235 二叉搜索树的最近公共祖先 描述 代码 237 删除链表中 ...
- LeetCode简单题目(#225 #226 #231 #232 #234)-5道(栈、队列、树、数字)
leetcode题库中共有350道简单题目. 本文记录已解决的题目和代码. 本文中的序号是leetcode题目中的真实序号. 文章目录 225 用队列实现栈 描述 代码 226 翻转二叉树 描述 代码 ...
最新文章
- redis 未授权访问详解
- boost::topological_sort用法的测试程序
- leetcode--114 二叉树展开为链表
- eclipse IDE中無法打開android模擬器
- python 读取并裁剪nc文件
- P2825 [HEOI2016/TJOI2016]游戏 (二分图最大匹配,预处理)
- ORA-00932: 数据类型不一致: 应为 -,但却获得 -
- Cpp--string常用函数用法总结
- 五个最佳FTP客户端工具
- 手机/iPad异地远程桌面控制Windows电脑【无公网IP】
- 家用无线路由器的选购技巧
- windows 剪贴板监控
- 如何将PDF文档转成Excel?
- 无聊写着玩:解二阶线性微分方程
- linux系统周几的格式是,linux cal命令显示日历信息周几天数差
- 俏兔子大战傻贼鹰(刚开始接触是写的,一直在草稿箱)
- 支付宝手机网站支付实战踩坑
- 原生js写篮球的运动轨迹!
- python怎么写入excel_Python写入Excel
- 创业失败身无分文,还欠债累累,靠父母的工资来生活,心情烦乱,我该何去何从?...