Leetcode Tags(4)Stack & Queue

  一、232. Implement Queue using Stacks

    private Stack<Integer> stack;

    /** Initialize your data structure here. */
    public e232() {
        stack = new Stack<>();
    }
    
    /** Push element x to the back of queue. */
    public void push(int x) {
        stack.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        Stack<Integer> tmp = new Stack<>();
        while (!stack.isEmpty()) tmp.push(stack.pop());
        int result = tmp.pop();
        while (!tmp.isEmpty()) stack.push(tmp.pop());
        return result;
    }
    
    /** Get the front element. */
    public int peek() {
        Stack<Integer> tmp = new Stack<>();
        while (!stack.isEmpty()) tmp.push(stack.pop());
        System.out.println(tmp);
        int result = tmp.peek();
        System.out.println(result);
        while (!tmp.isEmpty()) stack.push(tmp.pop());
        return result;
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        return stack.isEmpty();
    }
View Code

  二、225. Implement Stack using Queues

    private Queue<Integer> queue;

    /** Initialize your data structure here. */
    public e225() {
        queue = new ArrayDeque<>();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        queue.add(x);
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        Queue<Integer> tmp = new ArrayDeque<>();
        while (queue.size() != 1) tmp.add(queue.poll());
        int result = queue.peek();
        queue = tmp;
        return result;
        
    }
    
    /** Get the top element. */
    public int top() {
        Queue<Integer> tmp = new ArrayDeque<>();
        while (queue.size() != 1) {
            tmp.add(queue.poll());
        }
        int result = queue.peek();
        tmp.add(result);
        queue = tmp;
        return result;
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue.isEmpty();
    }
View Code

  三、155. Min Stack

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();   --> Returns -3.
minStack.pop();
minStack.top();      --> Returns 0.
minStack.getMin();   --> Returns -2.

  思路:使用两个栈,一个用来存值,另一个用来存在当前值压入栈后的最小值。

    private Stack<Integer> stack1 ;
    private Stack<Integer> stack2 ;
    private int min;

    /** initialize your data structure here. */
    public MinStack() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
        min = Integer.MAX_VALUE;
    }
    
    public void push(int x) {
        stack1.push(x);
        min = Math.min(min, x);
        stack2.push(min);
    }
    
    public void pop() {
        stack1.pop();
        stack2.pop();
        if (stack2.isEmpty()) {
            min = Integer.MAX_VALUE;
        } else {
            min = stack2.peek();
        }
    }
    
    public int top() {
        return stack1.peek();
    }
    
    public int getMin() {
        return stack2.peek();
    }
View Code

  四、739. Daily Temperatures

 

  五、

转载于:https://www.cnblogs.com/BigJunOba/p/9577266.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值