Design questions for LeetCode brushing questions (continuous update)

1. LRU caching mechanism

146. LRU Caching Mechanism

Use the data structure you know to design and implement an LRU (least recently used) cache mechanism. It should support the following operations: get data get and write data put.

  • Get data get(key)-If the key (key) exists in the cache, get the value of the key (always a positive number), otherwise return -1.
  • Write data put(key, value)-If the key does not exist, write its data value. When the cache capacity reaches the upper limit, it should delete the least recently used data value before writing new data to make room for the new data value.

Advanced:

Can you complete these two operations in O(1) time complexity?

示例:

LRUCache cache = new LRUCache( 2 /* 缓存容量 */ );

cache.put(1, 1);
cache.put(2, 2);
cache.get(1);       // 返回  1
cache.put(3, 3);    // 该操作会使得密钥 2 作废
cache.get(2);       // 返回 -1 (未找到)
cache.put(4, 4);    // 该操作会使得密钥 1 作废
cache.get(1);       // 返回 -1 (未找到)
cache.get(3);       // 返回  3
cache.get(4);       // 返回  4
/*
思路:
利用Map+Stack
 */
public class LRUCache {
    Map<Integer, Integer> map;
    Stack<Integer> stack;
    int size;

    public LRUCache(int capacity) {
        stack = new Stack<>();
        map = new HashMap<>(capacity);
        size = capacity;
    }

    public int get(int key) {
        if (!stack.contains(key)) {
            return -1;
        }
        stack.remove(Integer.valueOf(key));
        stack.push(key);
        return map.get(key);
    }

    public void put(int key, int value) {
        if (stack.contains(key)) {
            stack.remove(Integer.valueOf(key));
        } else {
            if (stack.size() == size) {
                int count = stack.remove(0);
                map.remove(count);
            }
        }
        stack.push(key);
        map.put(key, value);
    }
}

2. Minimal stack

155. Minimal stack

Design a stack that supports push, pop, and top operations and can retrieve the smallest element in a constant time.

  • push(x) - Push element x onto the stack.
  • pop() - Delete the element at the top of the stack.
  • top() - Get the top element of the stack.
  • getMin() - Retrieve the smallest element in the stack.
示例:

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();   --> 返回 -3.
minStack.pop();
minStack.top();      --> 返回 0.
minStack.getMin();   --> 返回 -2.
/*
思路:
维持两个栈:一个是元素压入栈;一个做最小元素压入栈,只要比栈顶元素小或者等于就压入
 */
public class MinStack {
    private Stack<Integer> pStack;
    private Stack<Integer> mStack;

    public MinStack() {
        pStack = new Stack<>();
        mStack = new Stack<>();
    }

    public void push(int x) {
        pStack.push(x);
        if (mStack.isEmpty() || mStack.peek() >= x) {
            mStack.push(x);
        }
    }

    public void pop() {
        int x = pStack.pop();
        if (x == mStack.peek()) mStack.pop();
    }

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

    public int getMin() {
        return mStack.peek();
    }

}

 

Guess you like

Origin blog.csdn.net/qq_34519487/article/details/104700549