zoukankan      html  css  js  c++  java
  • Leetcode146-lru-cache

    Leetcode146-lru-cache

      int capacity;
        int size;
        Map<Integer, ListNode> map = new HashMap<Integer, ListNode>();
        ListNode head;
        ListNode last;
    
        class ListNode {
            int key;
            int value;
            ListNode prev;
            ListNode next;
    
            public ListNode(int key, int val) {
                this.key = key;
                this.value = val;
                prev = null;
                next = null;
            }
    
            public int getKey() {
                return key;
            }
    
            public void setKey(int key) {
                this.key = key;
            }
    
            public int getValue() {
                return value;
            }
    
            public void setValue(int value) {
                this.value = value;
            }
        }
    
        public LRUCache(int capacity) {
            this.capacity = capacity;
        }
    
        public int get(int key) {
    
            if (map.containsKey(key)) {
                return key;
            }
            return -1;
        }
    
        public void put(int key, int value) {
    
            if (map.containsKey(key)) {
    
               moveNode(key, value);
    
            } else {
    
                if (size < capacity) {
                    insertNode(key, value);
                    size++;
                } else {
                    insertNode(key, value);
                    last = last.next;
                }
            }
        }
    
        public void insertNode(int key, int value) {
            ListNode node;
            node = new ListNode(key, value);
            node.next = head;
            head = node;
            ListNode current = last;
            while (current.next != null) {
                current = current.next;
            }
            current.next = node;
        }
    
        public void moveNode(int key, int value) {
            ListNode node;
            node = new ListNode(key, value);
            node.next = head;
            head = node;
            while (node.next.getKey() != key && node.next.getKey() != last.getKey()) {
                node = node.next;
            }
            node.next = node.next.next;
            ListNode current = last;
            while (current.next.getKey() != key && current.next.getKey() != last.getKey()) {
                current = current.next;
            }
            current.next = current.next.next;
        }
    

      

  • 相关阅读:
    python 读取邮件
    windows 关于时间的计算
    python 发送 smtp
    常用HTML富文本编辑器
    数据库设计:用户登录系统数据库表设计
    在当前页面打开一个固定的窗口(页面):这种方式弹出来的窗口进行表单提交可更新父类窗口
    后台模板
    springboot/springmvc转换器
    设计模式目录
    组合条件分页查询
  • 原文地址:https://www.cnblogs.com/Jomini/p/11997803.html
Copyright © 2011-2022 走看看