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();
 */

代码执行结果:


全部评论

相关推荐

02-26 09:15
已编辑
蚌埠学院 golang
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
10551次浏览 93人参与
# 你的实习产出是真实的还是包装的? #
1855次浏览 42人参与
# 巨人网络春招 #
11324次浏览 223人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7561次浏览 43人参与
# 简历第一个项目做什么 #
31664次浏览 335人参与
# 重来一次,我还会选择这个专业吗 #
433439次浏览 3926人参与
# MiniMax求职进展汇总 #
24007次浏览 309人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
187103次浏览 1122人参与
# 牛客AI文生图 #
21422次浏览 238人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152348次浏览 888人参与
# 研究所笔面经互助 #
118898次浏览 577人参与
# 简历中的项目经历要怎么写? #
310217次浏览 4210人参与
# AI时代,哪些岗位最容易被淘汰 #
63649次浏览 823人参与
# 面试紧张时你会有什么表现? #
30505次浏览 188人参与
# 你今年的平均薪资是多少? #
213074次浏览 1039人参与
# 你怎么看待AI面试 #
180035次浏览 1255人参与
# 高学历就一定能找到好工作吗? #
64324次浏览 620人参与
# 你最满意的offer薪资是哪家公司? #
76485次浏览 374人参与
# 我的求职精神状态 #
448043次浏览 3129人参与
# 正在春招的你,也参与了去年秋招吗? #
363373次浏览 2638人参与
# 腾讯音乐求职进展汇总 #
160638次浏览 1111人参与
# 校招笔试 #
470875次浏览 2964人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务