LeetCode专题----Design
2017-04-08 09:26:04         来源：changetocs的博客

# 146. LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

Could you do both operations in O(1) time complexity?

Example:

LRUCache cache = new LRUCache( 2 /* capacity */ );

cache.put(1, 1);
cache.put(2, 2);
cache.get(1); // returns 1
cache.put(3, 3); // evicts key 2
cache.put(4, 4); // evicts key 1
cache.get(3); // returns 3
cache.get(4); // returns 4

```public class LRUCache {
int key = 0;
int val = 0;
}

}

DLinkedNode pre = node.pre, next = node.next;
pre.next = next;
next.pre = pre;
}

removeNode(node);
}

removeNode(node);
return node;
}

private Map map = new HashMap<>();
private int capacity, count;

public LRUCache(int capacity) {
this.capacity = capacity;
count = 0;
}

public int get(int key) {
if(node == null) {
return -1;
}
return node.val;
}

public void put(int key, int value) {
if(node == null) {
newNode.key = key;
newNode.val = value;
map.put(key, newNode);
count++;
if(count > capacity) {
map.remove(tmp.key);
count--;
}
}
else {
node.val = value;
}
}
}

/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache obj = new LRUCache(capacity);
* int param_1 = obj.get(key);
* obj.put(key,value);
*/```

# LeetCode 155 : Min Stack (Java)

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

push(x) – Push element x onto stack.
pop() – Removes the element on top of the stack.
top() – Get the top element.
getMin() – Retrieve the minimum element in the stack.

LeetCode官网unlock的提示：
Hints:

Consider space-time tradeoff. How would you keep track of the minimums using extra space?
Make sure to consider duplicate elements.
O(n) runtime, O(n) space – Extra stack:

Use an extra stack to keep track of the current minimum value. During the push operation we choose the new element or the current minimum, whichever that is smaller to push onto the min stack.
O(n) runtime, O(n) space – Minor space optimization:

If a new element is larger than the current minimum, we do not need to push it on to the min stack. When we perform the pop operation, check if the popped element is the same as the current minimum. If it is, pop it off the min stack too.

```class MinStack {
private Stack stack = new Stack<>();
private Stack minStack = new Stack<>();

public void push(int x) {
if(minStack.isEmpty() || x <= minStack.peek())
minStack.push(x);
stack.push(x);
}

public void pop() {
if(stack.peek().equals(minStack.peek()))
minStack.pop();
stack.pop();
}

public int top() {
return stack.peek();
}

public int getMin() {
return minStack.peek();
}
}```