leetcode 栈、队列、堆相关

1、使用队列实现栈

https://leetcode.com/problems/implement-stack-using-queues/

class MyStack {

    private Queue<Integer> queue;
    
    /** Initialize your data structure here. */
    public MyStack() {
        queue = new LinkedList<Integer>();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        Queue<Integer> tempQueue = new LinkedList<Integer>();
        tempQueue.offer(x);
        while(queue.size() > 0){
            tempQueue.offer(queue.poll());
        }
        queue = tempQueue;
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return queue.poll();
    }
    
    /** Get the top element. */
    public int top() {
        return queue.peek();
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue.isEmpty();
    }
}

2、用栈实现队列

2.1、单栈法

https://leetcode.com/problems/implement-queue-using-stacks/

  • 除了 push 操作,其他操作就是直接使用栈的操作
  • 每次push 平均复杂度都是0(n)
class MyQueue {
    private Stack<Integer> stack;
    /** Initialize your data structure here. */
    public MyQueue() {
        stack = new Stack<Integer>();
    }
    
    /** Push element x to the back of queue. */
    public void push(int x) {
        Stack tempStack = new Stack<Integer>();
        while(!stack.isEmpty()){
            tempStack.push(stack.pop());
        }
        stack.push(x);
        while(!tempStack.isEmpty()){
            stack.push((Integer)tempStack.pop());
        }        
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        return stack.pop();
    }
    
    /** Get the front element. */
    public int peek() {
        return stack.peek();
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        return stack.isEmpty();
    }
}

2.2 双栈法

  • 平均复杂度0(1)
  • push 操作直接压栈 ,pop、peek 都需要判断 outputStack 为空的情况
class MyQueue {
    private Stack<Integer> inputStack;
    private Stack<Integer> outputStack;
    /** Initialize your data structure here. */
    public MyQueue() {
        inputStack = new Stack<Integer>();
        outputStack = new Stack<Integer>();
    }
    
    /** Push element x to the back of queue. */
    public void push(int x) {
        inputStack.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        if(outputStack.isEmpty()){
            while(!inputStack.isEmpty()){
                outputStack.push(inputStack.pop());
            }
        }
        return outputStack.pop();
    }
    
    /** Get the front element. */
    public int peek() {
        if(outputStack.isEmpty()){
            while(!inputStack.isEmpty()){
                outputStack.push(inputStack.pop());
            }
        }
        return outputStack.peek();
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        return inputStack.isEmpty() && outputStack.isEmpty();
    }
}

2.3 最小值栈

https://leetcode.com/problems/min-stack/

  • 用一个栈来存储正常栈的数据
  • 用另一个栈来存储 对栈每次操作下最小值的状态
class MinStack {

    public static Stack<Integer> stack;
    public static Stack<Integer> minStack;
    
    /** initialize your data structure here. */
    public MinStack() {
        stack = new Stack<Integer>();
        minStack = new Stack<Integer>();
    }
    
    public void push(int x) {
        stack.push(x);
        if(minStack.isEmpty()){
            minStack.push(x);
        }else{
            if(minStack.peek() > x){
                minStack.push(x);
            }else{
                minStack.push(minStack.peek());
            }
        }
    }
    
    public void pop() {
        stack.pop();
        minStack.pop();
    }
    
    public int top() {
        return stack.peek();
    }
    
    public int getMin() {
        return minStack.peek();
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */

2.4 数组中第K大的数

https://leetcode.com/problems/kth-largest-element-in-an-array/

  • 利用大小为K的小顶堆
  • java 中利用优先级队列模拟小顶堆
class Solution {
    public int findKthLargest(int[] nums, int k) {
        PriorityQueue<Integer> heap =
            new PriorityQueue<Integer>((n1, n2) -> n1 - n2);
        for(int i = 0;i < nums.length;i ++){
            heap.add(nums[i]);
            if(heap.size() > k){
                heap.poll();
            }
        }
        return heap.poll();
    }
}

2.5 寻找中位数

https://leetcode.com/problems/find-median-from-data-stream/

  • 利用大顶堆 存储有序数据的前半部分
  • 利用小顶堆 存储有序数据的后半部分
  • 这样中位数就可以从 两个堆的堆顶得到
class MedianFinder {
    PriorityQueue<Double> minHeap ;
    PriorityQueue<Double> maxHeap;
    
    /** initialize your data structure here. */
    public MedianFinder() {
        minHeap = new PriorityQueue<Double>((n1, n2) -> n1.compareTo(n2));
        maxHeap = new PriorityQueue<Double>((n1, n2) -> n2.compareTo(n1));
    }
    
    public void addNum(int num) {
        double dnum = (double)num;
        if(maxHeap.isEmpty()){
            maxHeap.add(dnum);
            return;
        }
        if(minHeap.size() == maxHeap.size()){
            if(dnum < maxHeap.peek()){
                maxHeap.add(dnum);
            }else {
                minHeap.add(dnum);
            }
            
        }else if(minHeap.size() > maxHeap.size()){
            if(dnum < minHeap.peek()){
                maxHeap.add(dnum);
            }else{
                maxHeap.add(minHeap.peek());
                minHeap.poll();
                minHeap.add(dnum);
            }
        }else if(minHeap.size() < maxHeap.size()){
            if(dnum <= maxHeap.peek()){
                minHeap.add(maxHeap.peek());
                maxHeap.poll();
                maxHeap.add(dnum);
            }else{
                minHeap.add(dnum);
            }
        }
    }
    
    public double findMedian() {
        if(minHeap.size() == maxHeap.size()){
            return (minHeap.peek() + maxHeap.peek())/2.0;
        }else if(minHeap.size() < maxHeap.size()){
            return maxHeap.peek();
        }else{
            return minHeap.peek();
        }
    }
}

/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder obj = new MedianFinder();
 * obj.addNum(num);
 * double param_2 = obj.findMedian();
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值