leetcode225.用队列实现栈,232.用栈实现队列

225题目链接
232题目链接

这两个数据结构设计类问题在我的文章 栈,队列,矩阵相关基础题目及答案 中已经有详细的解析了~

用队列实现栈题解:

本题思路如下:
用两个队列来实现栈这种结构,当向自己设计的栈结构push数据的时候,其中一个队列正常进行入队操作。当需要我们pop数据的时候,将有数据的那个队列除去队末的那个数字都enqueue(入队)到另一个队列中,并且让指向两个队列的引用交换。代码如下:

class MyStack {
    private Queue<Integer> dataQueue;
    private Queue<Integer> helpQueue;

    /** Initialize your data structure here. */
    public MyStack() {
        this.dataQueue = new LinkedList<>();
        this.helpQueue = new LinkedList<>();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        dataQueue.add(x);
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        if(dataQueue.isEmpty()){
            throw new RuntimeException("stack is empty");
        }
        while(dataQueue.size() != 1){
            helpQueue.add(dataQueue.poll());
        }
        int res = dataQueue.poll();
        Queue<Integer> tmp = dataQueue;
        dataQueue = helpQueue;
        helpQueue = tmp;
        return res;
    }
    
    /** Get the top element. */
    public int top() {
        if(dataQueue.isEmpty()){
            throw new RuntimeException("stack is empty");
        }
        while(dataQueue.size() != 1){
            helpQueue.add(dataQueue.poll());
        }
        int res = dataQueue.poll();
        Queue<Integer> tmp = dataQueue;
        dataQueue = helpQueue;
        helpQueue = tmp;
        dataQueue.add(res);
        return res;
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return dataQueue.isEmpty() && helpQueue.isEmpty();
    }
}

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

代码执行结果如下:


用栈实现队列题解:

使用两个栈来实现一个队列结构,其中一个栈为dataStack,另一个为helpStack。当队列发生进队操作时,向dataStack里push数据,当队列发生出队操作时,如果helpStack为空,就将dataStack依次pop出的数据push进helpStack中,然后返回helpStack pop的数据,如果helpStack不为空,就直接返回helpStack pop的数据,代码如下:

class MyQueue {
    private Stack<Integer> dataStack;
    private Stack<Integer> helpStack;

    /** Initialize your data structure here. */
    public MyQueue() {
        this.dataStack = new Stack<>();
        this.helpStack = new Stack<>();
    }
    
    /** Push element x to the back of queue. */
    public void push(int x) {
        dataStack.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        if(dataStack.isEmpty() && helpStack.isEmpty()){
            throw new RuntimeException("queue is empty");
        }
        if(helpStack.isEmpty()){
            while(!dataStack.isEmpty()){
                helpStack.push(dataStack.pop());
            }
        }
        return helpStack.pop();
    }
    
    /** Get the front element. */
    public int peek() {
        if(dataStack.isEmpty() && helpStack.isEmpty()){
            throw new RuntimeException("queue is empty");
        }
        if(helpStack.isEmpty()){
            while(!dataStack.isEmpty()){
                helpStack.push(dataStack.pop());
            }
        }
        return helpStack.peek();
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        return dataStack.isEmpty() && helpStack.isEmpty();
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */

代码执行结果:


全部评论

相关推荐

点赞 评论 收藏
分享
01-30 22:03
门头沟学院 Java
用微笑面对困难:我滴妈,【俩月】【实习】【主管】仨debuff吃满了,独立设计开发的项目写了绝大占比的运营板块,你独立开发,那维护、问题复盘、日志更新、bug、策划书全是自己整的? 不建议写那么大,可以从小出发更容易
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
8359次浏览 76人参与
# 你的实习产出是真实的还是包装的? #
1545次浏览 39人参与
# 巨人网络春招 #
11281次浏览 223人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7300次浏览 40人参与
# 简历第一个项目做什么 #
31444次浏览 320人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186719次浏览 1118人参与
# MiniMax求职进展汇总 #
23601次浏览 305人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152201次浏览 887人参与
# 研究所笔面经互助 #
118827次浏览 577人参与
# 重来一次,我还会选择这个专业吗 #
433235次浏览 3926人参与
# 简历中的项目经历要怎么写? #
309862次浏览 4177人参与
# 面试紧张时你会有什么表现? #
30460次浏览 188人参与
# 你今年的平均薪资是多少? #
212910次浏览 1039人参与
# AI时代,哪些岗位最容易被淘汰 #
63173次浏览 784人参与
# 我的求职精神状态 #
447925次浏览 3128人参与
# 你最满意的offer薪资是哪家公司? #
76352次浏览 374人参与
# 正在春招的你,也参与了去年秋招吗? #
363045次浏览 2635人参与
# 你怎么看待AI面试 #
179691次浏览 1216人参与
# 牛客AI文生图 #
21391次浏览 237人参与
# 职能管理面试记录 #
10773次浏览 59人参与
# 网易游戏笔试 #
6422次浏览 83人参与
# 腾讯音乐求职进展汇总 #
160527次浏览 1109人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务