[LeetCode]LRU Cache,leetcodelrucache

來源:互聯網
上載者:User

[LeetCode]LRU Cache,leetcodelrucache

題目:實現一個LRU Cache

演算法:

  • 雙向鏈表 + HashMap
  • get:若節點不存在,返回-1;否則返回節點value,並將節點調整到head
  • set(key, value):
    • 若key已經存在:更新value,並將節點調整到head;
    • 若key不存在:若cache容量足夠,將節點壓入鏈表頭部;若cache容量不足,淘汰鏈表尾部節點,並將新節點壓入鏈表頭部


import java.util.HashMap;public class LRUCache {    /**     * Structure     *      * @author ouyangyewei     */    public class CacheNode {        public int key;        public int value;        public CacheNode prev;        public CacheNode next;                public CacheNode() {            // nothing        }        public CacheNode(int key, int value) {            this.key = key;            this.value = value;            this.prev = null;            this.next = null;        }    }        private int capacity;    private int currentCapacity;    private CacheNode head;    private CacheNode tail;    private HashMap<Integer, CacheNode> hashNode;        public LRUCache(int capacity) {        this.capacity = capacity;        this.currentCapacity = 0;        this.head = null;        this.tail = null;        this.hashNode = new HashMap<Integer, CacheNode>();    }        public int get(int key) {        CacheNode node = hashNode.get(key);        if (null == node) {            return -1;        } else {            moveToHead(node);            return node.value;        }    }        public void set(int key, int value) {        if (null == hashNode.get(key)) {            /** cacheNode is not exists! */            CacheNode node = new CacheNode(key, value);            if (currentCapacity < capacity) {                /** enough capacity for add a node */                addToHead(node);                ++currentCapacity;            } else {                /** over the maximum capacity */                removeLeastUseNode();                addToHead(node);                currentCapacity = capacity;            }                        hashNode.put(key, node);        } else {            /** cacheNode is already exists */            CacheNode node = hashNode.get(key);            node.value = value;            moveToHead(node);            hashNode.put(key, node);        }    }        /**     * Move a node to list's head     * @param node     */    public void moveToHead(CacheNode node) {        if (null != node.prev) {            /** node locate behind the list's head */            CacheNode front = node.prev;            CacheNode behind = node.next;            if (null == behind) {                /** node locate at the list's tail */                front.next = behind;                tail = front;            } else {                front.next = behind;                behind.prev = front;            }                        node.prev = null;            node.next = head;            head.prev = node;            head = node;        }    }        /**     * Add a node to list's head.     * @param node     */    public void addToHead(CacheNode node) {        if (null == head) {            /** list is empty */            head = node;            tail = node;        } else {            /** list is not empty */            node.next = head;            head.prev = node;            head = node;        }    }        /**     * Remove the least use node, it means remove      * the list's tail node.     */    public void removeLeastUseNode() {        if (null != tail.prev) {            hashNode.remove(tail.key);            tail = tail.prev;            tail.next = null;        } else {            /** list has only one node */            hashNode.remove(tail.key);            head = null;            tail = null;        }    }    //    public static void main(String[] args) {//        LRUCache lruCache = new LRUCache(4);//        lruCache.set(1, 1);//        lruCache.set(0, 0);//        lruCache.get(1);//        lruCache.get(0);//        lruCache.set(2, 2);//        lruCache.set(4, 4);//        lruCache.get(1);//        lruCache.get(0);//        lruCache.get(0);//        lruCache.set(8, 8);//        lruCache.set(7, 7);//        lruCache.set(5, 5);//        lruCache.set(4, 4);//        lruCache.set(3, 3);//        lruCache.set(2, 2);//        lruCache.get(3);//        lruCache.get(4);//        LRUCache lruCache = new LRUCache(1);//        lruCache.set(2, 1);//        System.out.println(lruCache.get(2));//        lruCache.set(3, 2);//        System.out.println(lruCache.get(2));//        System.out.println(lruCache.get(3));//        LRUCache lruCache = new LRUCache(2);//        lruCache.set(2, 1);//        lruCache.set(1, 2);//        lruCache.set(2, 3);//        lruCache.set(4, 1);//        System.out.println(lruCache.get(1));//        System.out.println(lruCache.get(2));//    }}


leetcode中multiply strings出現runtime error,解

leetcode的定位是面試題,不是ACM題
也就是說題目並不是以AC與否來判斷你的答案正確與否的
更重要的是面試的時候給別人的感覺

這本來是一道考察字串表示大數和大叔乘法的題,po主卻用上了stringstream和vector
在面試裡面這樣的答案哪怕最後能得到正確的結果,也不會給面試官留下好的印象。

我會幫你偵錯工具,但是我還是覺得你重新寫一份簡單易懂的更好,避免使用STL和流
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.