Leetcode 刷题日记 2021.2.7

Leetcode 刷题日记
2021.2.7

题目链接:
https://leetcode-cn.com/problems/implement-stack-using-queues/submissions/

问题描述:
用队列实现栈
在这里插入图片描述

解答1:
双队列实现

代码:

import java.util.LinkedList;

class MyStack {

    LinkedList<Integer> queue1 = new LinkedList<>();
    LinkedList<Integer> queue2 = new LinkedList<>();

    /** Initialize your data structure here. */
    public MyStack() {

    }

    /** Push element x onto stack. */
    public void push(int x) {
        if(queue1.isEmpty()){
            queue1.addFirst(x);
            pour(queue2,queue1);
        }else if(queue2.isEmpty()){
            queue2.addFirst(x);
            pour(queue1,queue2);
        }
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        if(!queue1.isEmpty()) return queue1.removeFirst();
        else if(!queue2.isEmpty()) return queue2.removeFirst();
        return -1;
    }

    /** Get the top element. */
    public int top() {
        if(!queue1.isEmpty()) return queue1.getFirst();
        else if(!queue2.isEmpty()) return queue2.getFirst();
        return -1;
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue1.isEmpty() && queue2.isEmpty();
    }

    private static void pour(LinkedList<Integer> from,LinkedList<Integer> to){
        while(!from.isEmpty()){
            to.addLast(from.removeFirst());
        }
    }
}

 

分析:
时间复杂度:
push():O(n)
pop():O(1)
top:O(1)
empty:O(1)
空间复杂度:
push():O(n)
pop():O(n)
top:O(n)
empty:O(n)

运行结果:
在这里插入图片描述

评注:
这和使用两个栈实现队列有相似之处
(利用栈实现队列的题目链接:https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/
该题博文链接:
https://blog.csdn.net/m0_51404298/article/details/113744309

这道题的意义就是建立起栈和队列的联系,用辩证统一的观点来看待问题。

解答2:
单队列实现(不符合题意但更加简单)

代码:

import java.util.LinkedList;

class MyStack {

    LinkedList<Integer> queue = new LinkedList<>();


    /** Initialize your data structure here. */
    public MyStack() {

    }

    /** Push element x onto stack. */
    public void push(int x) {
        queue.push(x);
            for (int i = 0; i < queue.size() - 1; i++) {
                queue.push(queue.removeFirst());
            }
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return queue.removeFirst();
    }

    /** Get the top element. */
    public int top() {
        return queue.getFirst();
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue.isEmpty();
    }

    public static void main(String[] args) {
        MyStack stack = new MyStack();
        stack.push(1);
        stack.push(2);
        System.out.println(stack.top());
    }

}


分析:
时间复杂度:
push():O(n)
pop():O(1)
top:O(1)
empty:O(1)
空间复杂度:
push():O(n)
pop():O(n)
top:O(n)
empty:O(n)

运行结果:
在这里插入图片描述

评注:
实际上用一个队列就能实现栈,思路就是先让被压入的元素入队,然后让前面的元素依次出队后再入队。这样就能保证push()和top()都是常数时间开销。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值