当前位置: 首页 > news >正文

对网站建设有什么样好的建设意见手工制作帽子 小学生

对网站建设有什么样好的建设意见,手工制作帽子 小学生,百度 营销推广怎么做,网站建设加空间146. LRU 缓存 请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。 实现 LRUCache 类#xff1a; LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存int get(int key) 如果关键字 key 存在于缓存中#xff0c;则返回关键字的值#xff0…146. LRU 缓存 请你设计并实现一个满足  LRU (最近最少使用) 缓存 约束的数据结构。 实现 LRUCache 类 LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存int get(int key) 如果关键字 key 存在于缓存中则返回关键字的值否则返回 -1 。void put(int key, int value) 如果关键字 key 已经存在则变更其数据值 value 如果不存在则向缓存中插入该组 key-value 。如果插入操作导致关键字数量超过 capacity 则应该 逐出 最久未使用的关键字。 函数 get 和 put 必须以 O(1) 的平均时间复杂度运行。 示例 输入 [LRUCache, put, put, get, put, get, put, get, get, get] [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]] 输出 [null, null, null, 1, null, -1, null, -1, 3, 4]解释 LRUCache lRUCache new LRUCache(2); lRUCache.put(1, 1); // 缓存是 {11} lRUCache.put(2, 2); // 缓存是 {11, 22} lRUCache.get(1); // 返回 1 lRUCache.put(3, 3); // 该操作会使得关键字 2 作废缓存是 {11, 33} lRUCache.get(2); // 返回 -1 (未找到) lRUCache.put(4, 4); // 该操作会使得关键字 1 作废缓存是 {44, 33} lRUCache.get(1); // 返回 -1 (未找到) lRUCache.get(3); // 返回 3 lRUCache.get(4); // 返回 4提示 1 capacity 30000 key 100000 value 105最多调用 2 * 105 次 get 和 put 解法1Map ArrayList (key) 用一个Map来存放key和value一个ArrayList来存放访问顺序。 当用户get、put的key存在时则从ArrayList中找到对应的key删除然后把新访问的key放到尾部。 头部表示最久未访问的数据尾部表示最新访问的数据。与数据结构匹配。 class LRUCache {private int capacity;private MapInteger,Integer cache;private ListInteger delete;public LRUCache(int capacity) {this.cache new HashMap(capacity);this.capacity capacity;this.delete new ArrayList(capacity);}public int get(int key) {Integer value cache.get(key);if(value ! null){// update to delete queuedelete.remove((Integer)key);delete.add((Integer)key);return value;}return -1;}public void put(int key, int value) {// if existedif (cache.containsKey(key)) {// update to delete queuedelete.remove((Integer)key);delete.add((Integer)key);cache.put(key, value);return;}// if cache is full, need to deleteif(cache.size() capacity){Integer deleteKey delete.remove(0);cache.remove(deleteKey);}cache.put(key, value);delete.add(key);}}/*** Your LRUCache object will be instantiated and called as such:* LRUCache obj new LRUCache(capacity);* int param_1 obj.get(key);* obj.put(key,value);*/ 性能不怎么好花了883ms。大部分时间花在ArrayList中查找key的位置和删除key后移动元素上了。 那我们想办法改进下查找和删除。 解法2Map ArrayList (key, timestamp) 我们看到解法1中大部分时间都是在ArrayList中查找key和删除后移动数据。那么我们能不能不移动数据答案是可以的。 方法是我们每次操作key后为其引入一个timestamp需要删除这个key时比较待删除的timestamp是否没有更新过没更新过则删除。这里使用程序启动后的纳秒防止在同一个微秒内完成了多个操作不能区分。 class LRUCache {private int capacity;private MapInteger,KeyTime cache;private ListKeyTime delete;public LRUCache(int capacity) {this.capacity capacity;this.cache new HashMap(capacity*3/2);this.delete new LinkedList();}public int get(int key) {KeyTime kt cache.get(key);if(kt ! null){long time System.nanoTime();kt.time time;// update to delete queuedelete.add(new KeyTime(key, kt.value, time));return kt.value;}return -1;}public void put(int key, int value) {long time System.nanoTime();// if existedKeyTime kt cache.get(key);if (kt ! null) {kt.time time;kt.value value;// update to delete queuedelete.add(new KeyTime(key, value, time));return;}// if cache is full, need to deleteif(cache.size() capacity){while(true) {KeyTime deleteKt delete.remove(0);KeyTime existKt cache.get(deleteKt.key);if (existKt ! null existKt.time deleteKt.time) {cache.remove(deleteKt.key);break;}}}cache.put(key, new KeyTime(key, value, time));delete.add(new KeyTime(key, value, time));}static class KeyTime {int key;int value;long time;public KeyTime(int key, int value, long time) {this.key key;this.value value;this.time time;}}}/*** Your LRUCache object will be instantiated and called as such:* LRUCache obj new LRUCache(capacity);* int param_1 obj.get(key);* obj.put(key,value);*/ 性能已经从883ms提升到了52ms历史性的飞跃。还不错可惜才28.88%要是还想进一步优化的话可以参考官方的LinkedHashMap实现。   解法3HashMap 双向链表 LinkedHashMap 这里get/put 后把新访问的节点移动到表尾处表头存放的是最久未访问的数据。 具体实现如下   public class LRUCache {/*** use double direction linked list to store the order which to evict over due key.*/private int capacity;private MapInteger,Node cache;// store the first node to delete which is unused for a long timeprivate Node head;// store the used node recentlyprivate Node tail;public LRUCache(int capacity) {this.capacity capacity;this.cache new HashMap(capacity);this.head new Node(-1, 0, null, null);this.tail new Node(-2, 0, head, null);this.head.next tail;}public int get(int key) {Node node cache.get(key);if (node ! null) {// existed then move to headmoveToTail(node);return node.value;}return -1;}public void put(int key, int value) {Node node cache.get(key);if (node ! null) {// existed, only need to update positionmoveToTail(node);node.value value;return;}// cache is full, get the last unused node to removeif (cache.size() capacity) {// remove the head node which is unused for a long timeNode toDelete head.next;deleteNode(toDelete);cache.remove(toDelete.key);}node new Node(key, value, null, null);cache.put(key, node);addToTail(node);}static class Node {int key;int value;Node previous;Node next;public Node(int key, int value, Node previous, Node next) {this.key key;this.value value;this.previous previous;this.next next;}}public void moveToTail(Node node) {deleteNode(node);addToTail(node);}/*** delete node from list.*/public void deleteNode(Node node) {node.previous.next node.next;node.next.previous node.previous;}/*** add node to list tail.*/public void addToTail(Node node) {node.next tail;node.previous tail.previous;tail.previous.next node;tail.previous node;} }运行时间43ms超越98.98%可以了。
http://www.pierceye.com/news/490315/

相关文章:

  • 三只松鼠的网站建设理念桐庐营销型网站建设
  • 建设银行网站未响应大理如何做百度的网站
  • 广州建立公司网站多少钱页面跳转不了怎么回事
  • 爱做的小说网站吗百度权重高的发帖网站
  • 做网站的空间费用要多少产品怎么做推广和宣传
  • 商城网站制作明细老牌深圳公司大雨中解散
  • wordpress缩略图设置百度站长工具seo
  • 建站还有前途么食品包装设计规范及包装标准
  • 专门做漫画的网站企业网站改版seo
  • 最新网站建设合同做网站在哪里添加关键词
  • 集团网站开发多少钱做网站不难吧
  • 全总基层组织建设网站百度录入网站
  • 网站建设的实验步骤wordpress linux 建站教程
  • 哪个网站专门做邮轮旅游的加拿大28平台微信
  • 网站设置的用途wordpress 5.1 运行环境
  • 中小企业服务中心网站建设网站域名中文后缀
  • 龙武工会网站怎么做5173游戏交易网站源码
  • 网站建设设计时代创信好海南城乡和住房建设厅网站
  • 大连最好的做网站的公司崇义做网站
  • 圣弘建设股份有限公司网站上海图文设计有限公司
  • gta5资产网站正在建设零基础自学设计
  • 深圳专业制作网站公司吗网站信息化建设报送
  • 苏州网站建设运营推广网站一年多少钱?
  • WordPress国外主机湖北短视频seo营销
  • 南通网站建设电话设计一个网站要多少钱
  • 好的模板网站建设网站规划 时间
  • 昆明seocn整站优化网站建设如何报价
  • 网页设计模板免费网站WordPress生成网站地图
  • 做网站 侵权做外贸怎么看外国网站
  • 网站建设知识点的总结普通网站建设是什么