码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构

时间:2017-08-28 23:42:35      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:div   sem   lap   minstack   ash   capacity   nod   poll   i++   

1 Min Stack

技术分享
public class MinStack {
    private Stack<Integer> stack = new Stack();
    private Stack<Integer> min = new Stack();
    public MinStack() {
        // do initialize if necessary
    }

    public void push(int number) {
        // write your code here
        if (stack.isEmpty()) {
            stack.push(number);
            min.push(number);
        } else {
            if (number < min.peek()) {
                min.push(number);
            } else {
                min.push(min.peek());
            }
            stack.push(number);
        }
    }

    public int pop() {
        min.pop();
        return stack.pop();
    }

    public int min() {
        return min.peek();
    }
}
View Code

 2 Implement Queue by Two Stacks

技术分享
public class MyQueue {
    private Stack<Integer> in;
    private Stack<Integer> out;

    public MyQueue() {
       // do initialization if necessary
       in = new Stack<>();
       out = new Stack<>();
    }
    
    public void push(int element) {
        // write your code here
        in.push(element);
    }

    public int pop() {
        // write your code here
        if (out.isEmpty()) {
            inToOut();
        }
        return out.pop();
    }

    public int top() {
        // write your code here
        if (out.isEmpty()) {
            inToOut();
        }
        return out.peek();
    }
    
    void inToOut() {
        while (!in.isEmpty()) {
            out.push(in.pop());
        }
    }
}
View Code

3 Rehashing

技术分享
    public ListNode[] rehashing(ListNode[] hashTable) {
        // write your code here
        if (hashTable == null || hashTable.length == 0)
        {
            return hashTable;
        }
        int newCapacity = hashTable.length * 2;
        ListNode[] newTable = new ListNode[newCapacity];
        for (int i = 0; i < hashTable.length; i++) {
            while (hashTable[i] != null) {
                int index = (hashTable[i].val % newCapacity + newCapacity) % newCapacity;
                if (newTable[index] == null) {
                    newTable[index] = new ListNode(hashTable[i].val);
                } else {
                    ListNode dummy = newTable[index];
                    while (dummy.next != null) {
                        dummy = dummy.next;
                    }
                    dummy.next = new ListNode(hashTable[i].val);
                }
                hashTable[i] = hashTable[i].next;
            }
        }
        return newTable;
    }
View Code

4 Data Stream Median

技术分享
public class MedianFinder {

    /** initialize your data structure here. */
    Queue<Integer> minQueue;
    Queue<Integer> maxQueue;
    public MedianFinder() {
        Comparator<Integer> comparator = new Comparator<Integer>(){
            public int compare(Integer left, Integer right) {
                return right - left;
            }  
        };
        minQueue = new PriorityQueue<>();
        maxQueue = new PriorityQueue<>(comparator);
    }
    
    public void addNum(int num) {
        maxQueue.add(num);
        minQueue.add(maxQueue.poll());
        if (maxQueue.size() < minQueue.size()) {
            maxQueue.add(minQueue.poll());
        }
    }
    
    public double findMedian() {
        return (double)(maxQueue.size() > minQueue.size() ? maxQueue.peek() : (minQueue.peek() + maxQueue.peek()) / 2.0);
    }
}
View Code

5 LRU cache

技术分享
public class LRUCache {

     class Node {
        int key;
        int val;
        Node pre;
        Node next;
        
        public Node(int key, int val) {
            this.key = key;
            this.val = val;
            this.pre = null;
            this.next = null;
        }
    }
    
    private int capacity;
    private Map<Integer, Node> map = new HashMap<>();
    private Node head = new Node(-1, -1);
    private Node tail = new Node(-1, -1);
    
    public LRUCache(int capacity) {
        this.capacity = capacity;
        head.next = tail;
        tail.pre = head;
    }

    public int get(int key) {
        if (!map.containsKey(key)) {
            return -1;
        }
        Node current = map.get(key);
        current.next.pre = current.pre;
        current.pre.next = current.next;
        
        move_to_tail(current);
        
        return current.val;
    }
    
    void move_to_tail(Node node) {
        Node pre = tail.pre;
        pre.next = node;
        node.pre = pre;
        node.next = tail;
        tail.pre = node;
    }
    public void set(int key, int value) {
        if (get(key) != -1) {
            map.get(key).val = value;
            return;
        }
        if (map.size() == capacity) {
            map.remove(head.next.key);
            head.next = head.next.next;
            head.next.pre = head;
        }
        
        Node insert = new Node(key, value);
        map.put(key, insert);
        move_to_tail(insert);
    }
    
}
View Code

 

数据结构

标签:div   sem   lap   minstack   ash   capacity   nod   poll   i++   

原文地址:http://www.cnblogs.com/whesuanfa/p/7442082.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!