标签: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(); } }
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()); } } }
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; }
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); } }
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); } }
标签:div sem lap minstack ash capacity nod poll i++
原文地址:http://www.cnblogs.com/whesuanfa/p/7442082.html