zoukankan      html  css  js  c++  java
  • 数据结构

    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
  • 相关阅读:
    序列合并(luogu 1631)题解
    邮递员送信(luogu 1629)题解
    敲砖块(codevs 1257)题解
    货车运输(codevs 3287)题解
    分布式服务框架 Zookeeper -- 管理分布式环境中的数据
    dubbo学习
    第一章 1.20 多线程基础
    第一章 1.19 网络编程基础
    练习 : 面向对象
    练习 : 正则表达式
  • 原文地址:https://www.cnblogs.com/whesuanfa/p/7442082.html
Copyright © 2011-2022 走看看