1. 程式人生 > 實用技巧 >703. Kth Largest Element in a Stream

703. Kth Largest Element in a Stream

node類

class Node{
    private int v;
    
    private Object  k;
    private Node next;
    private Node pre;
    
    public Node(Object key,int value) {
        this.v = value;
        this.k = key;
    }
    
    public int getV() {
        return v;
    }
    public void setV(int v) {
        this
.v = v; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } public Node getPre() { return pre; } public void setPre(Node pre) { this.pre = pre; } public Object getK() { return k; }
public void setK(Object k) { this.k = k; } }

LRUcache類

public class LRUCache_NowCode {
    public static Object[] LRU(int[][] operators, int k) {
        if (operators.length <= 0 || k <= 0) {
            return null;
        }
        List<Object> reList = new ArrayList<Object>();
        LRUCache_NowCode cache 
= new LRUCache_NowCode(k); for (int[] tmpArr : operators) { int key = tmpArr[0]; switch (key) { case 1: cache.set(Integer.valueOf(tmpArr[1]), Integer.valueOf(tmpArr[2])); break; case 2: reList.add(cache.get(Integer.valueOf(tmpArr[1]))); break; default: break; } } Object[] reArr = new Object[reList.size()]; for (int i = 0; i < reList.size(); i++) { reArr[i] = reList.get(i); } return reArr; } public LRUCache_NowCode(int k) { this.size = k; } private int size = -1; // 容量 private Node head = null; private Node tail = null; private Map<Object, Node> cacheMap = new HashMap<Object, Node>(); public void set(Object key, int value) { System.out.println("set key -> " + key + " value -> " + value); Node node = cacheMap.get(key); if (node == null) { node = new Node(key,value); cacheMap.put(key, node); } else { // 將節點從連結串列中移除 if (node.getNext() != null) { node.getNext().setPre(node.getPre()); } if (node.getPre() != null) { node.getPre().setNext(node.getNext()); } node.setV(value); } // 頭尾節點給預設值 if (head == null || tail == null) { head = tail = node; } // 容量判斷 if (cacheMap.size() > size) { //移除尾指標 cacheMap.remove(tail.getK()); //移動尾指標 tail = tail.getPre(); tail.setNext(null); } // 當前節點不是頭結點 if (head != node) { head.setPre(node); node.setNext(head); node.setPre(null); head = node; } printHead(head); printTail(tail); System.out.println("-----"); } public static void printHead(Node node) { System.out.print(node.getV()); Node tmp = node; while (tmp.getNext() != null) { tmp = tmp.getNext(); System.out.print(" next -> " + tmp.getV()); } System.out.println(); } public static void printTail(Node node) { System.out.print(node.getV()); Node tmp = node; while (tmp.getPre() != null) { tmp = tmp.getPre(); System.out.print(" pre -> " + tmp.getV()); } System.out.println(); } /** * 移到佇列頭部 * * @param i * @return */ public Object get(Object key) { System.out.println("get key -> " + key); Node existNode = cacheMap.get(key); if (existNode == null) { return -1; } Node n = existNode.getNext(); Node p = existNode.getPre(); if (p != null) { p.setNext(n); } if (n != null) { n.setPre(p); } existNode.setNext(head); head.setPre(existNode); existNode.setPre(null); head = existNode; // 如果恰好查的是尾節點,那將尾指標前移一個單位 if (tail == existNode) { tail = p; tail.setNext(null); } return existNode.getV(); } }

main方法測試

public static void main(String[] args) {
        int[][] operators = new int[][] { 
        new int[] { 1, 1, 1 }, 
        new int[] { 1, 2, 2 }, 
        new int[] { 1, 3, 2 },
    new int[] { 2, 1 }, 
        new int[] { 1, 4, 4 }, 
        new int[] { 2, 2 } };
    Object[] arr = LRUCache_NowCode.LRU(operators, 3);
    System.out.println(Arrays.toString(arr));
    }        

此處直接將key儲存在了物件中,可以根據物件之間的引用關係直接找到前後的key,簡化處理頭尾節點時邏輯。