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();}}

posted on 2015-04-24 23:15 Jackiesteed 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/jackiesteed/articles/4454906.html

leetcode LRUCache题目相关推荐

  1. 【持续更新】Leetcode SQL题目全解析(附建表sql)

    Leetcode SQL题目全解析 越前须知(雾) 题目Q & A 175 组合两个表 181 超过经理收入的员工 182 查找重复电子邮箱 183 从不订购的用户 197 上升的温度 511 ...

  2. LeetCode数据库题目1-123

    LeetCode数据库题目1-123 175. 组合两个表 难度简单 SQL架构 表1: Person +-------------+---------+ | 列名 | 类型 | +--------- ...

  3. 算法思想-深度搜索算法-leetcode相关题目总结

    通过这篇文章你能学到什么 搜索算法 深度优先搜索 分析过程 实现代码 进出栈过程示意图 DFS算法应用-Leetcode相关题目 Leetcode 78 Subsets Leetcode 90 Sub ...

  4. LeetCode 所有题目总结

    文章目录 做题注意事项 题目分类 1.位运算 2.字符串题型 3.TopK 问题--最大堆/最小堆 4.链表 5.动态规划 easy Medium hard 6.贪心 7.树 8.图 9.数学题 10 ...

  5. java 套娃_【leetcode编程题目354】俄罗斯套娃

    来自 https://leetcode.com/problems/russian-doll-envelopes/ You have a number of envelopes with widths ...

  6. 【LeetCode刷题记录】LeetCode经典题目数组求和及哈希表的使用!

    点上方蓝字计算机视觉联盟获取更多干货 在右上方 ··· 设为星标 ★,与你不见不散 LeetCode题目 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整 ...

  7. LeetCode简单题目(#263 #268 #278 #283 #290)-5道(数字、字符串)

    leetcode题库中共有350道简单题目. 本文记录已解决的题目和代码. 本文中的序号是leetcode题目中的真实序号. 文章目录 263 丑数 描述 代码 大神代码 268 缺失数字 描述 代码 ...

  8. LeetCode简单题目(#235 #237 #242 #257 #258)-5道(树、数字、字符串)

    leetcode题库中共有350道简单题目. 本文记录已解决的题目和代码. 本文中的序号是leetcode题目中的真实序号. 文章目录 235 二叉搜索树的最近公共祖先 描述 代码 237 删除链表中 ...

  9. LeetCode简单题目(#225 #226 #231 #232 #234)-5道(栈、队列、树、数字)

    leetcode题库中共有350道简单题目. 本文记录已解决的题目和代码. 本文中的序号是leetcode题目中的真实序号. 文章目录 225 用队列实现栈 描述 代码 226 翻转二叉树 描述 代码 ...

最新文章

  1. redis 未授权访问详解
  2. boost::topological_sort用法的测试程序
  3. leetcode--114 二叉树展开为链表
  4. eclipse IDE中無法打開android模擬器
  5. python 读取并裁剪nc文件
  6. P2825 [HEOI2016/TJOI2016]游戏 (二分图最大匹配,预处理)
  7. ORA-00932: 数据类型不一致: 应为 -,但却获得 -
  8. Cpp--string常用函数用法总结
  9. 五个最佳FTP客户端工具
  10. 手机/iPad异地远程桌面控制Windows电脑【无公网IP】
  11. 家用无线路由器的选购技巧
  12. windows 剪贴板监控
  13. 如何将PDF文档转成Excel?
  14. 无聊写着玩:解二阶线性微分方程
  15. linux系统周几的格式是,linux cal命令显示日历信息周几天数差
  16. 俏兔子大战傻贼鹰(刚开始接触是写的,一直在草稿箱)
  17. 支付宝手机网站支付实战踩坑
  18. 原生js写篮球的运动轨迹!
  19. python怎么写入excel_Python写入Excel
  20. 创业失败身无分文,还欠债累累,靠父母的工资来生活,心情烦乱,我该何去何从?...

热门文章

  1. 算法题---最长公共前缀
  2. 【快速入门ORM框架之Dapper】大牛勿进系列
  3. 运用循环判断语句和列表的购物车程序
  4. Unity3D之FSM有限状态机
  5. 深入Java集合学习系列:TreeMap实现
  6. IntelliJ IDEA 编辑器配置vue高亮显示
  7. 前端性能优化的重要方案:图片懒加载
  8. css3实现的一些灰色的导航条按钮
  9. HTML-盒子模型(padding-margining)-样式继承-浮动
  10. 交互系统的构建之(三)TTS语音合成的加盟