用双栈实现队列(java)

本文介绍了一种使用双栈实现队列的方法,通过将入队操作分配给输入栈,出队操作从输出栈进行,实现了高效的数据获取。同时,探讨了双队列实现栈的策略,确保栈顶元素始终位于output队列首位,从而模拟栈的特性。文章详细解释了这两种数据结构转换的原理及具体代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述在这里插入图片描述out 栈相当于一个缓冲区域,把一部分入队的放到这个缓冲区域
当你拿出头元素的时候就可以直接去缓冲区去拿了,效率高,
LeetCode官网还给出了第二种解法,用到了均摊复杂度的知识,展示还没搞懂,这里介绍是方法一


class MyQueue {
    
    Stack<Integer> input;
    Stack<Integer> output;

    /** Initialize your data structure here. */
    public MyQueue() {
        input = new Stack();
        output = new Stack();
        
        
    }
    
    /** Push element x to the back of queue. */
    public void push(int x) {
        input.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        if(!output.isEmpty()) {
           return output.pop();
        }else {
            while(!input.isEmpty()) {
                 output.push(input.pop());
            }
            return output.pop();
            
        }
        
    }
    
    /** Get the front element. */
    public int peek() {
        
        if(!output.isEmpty()) {
            return output.peek();
        }else {
            
            while(!input.isEmpty()) {
                output.push(input.pop());
            }
            
            return output.peek();
            
            
                
        }
        
        
        
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        return output.isEmpty()&&input.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();
 */





下面是双队列实现

队列实现栈

我对这道题的理解是,准备两个队列,我分别命名为 input,队列和output 队列

output队列的第一位始终是最后一个进队列的,因此,每一次弹栈,都是弹出最后那个进队列的,这样就可以模拟栈的特性了 具体代码如下 `


class MyStack {

Queue<Integer> input;
Queue<Integer> output;
/** Initialize your data structure here. */
public MyStack() {
    
    input = new LinkedList();
    output = new LinkedList();
    
    
    
    
}

/** Push element x onto stack. */
public void push(int x) {
    
    if(output.size()==0) {
        output.offer(x);
    }else {
        while(output.size()!=0) {
            input.offer(output.poll());//清空 output 队列
        }
            output.offer(x);           //进队的放在output队列的第一位
        while(input.size()!=0) {
            output.offer(input.poll());//重改output 队列
        }
    }
    
    
    
    
}

/** Removes the element on top of the stack and returns that element. */
public int pop() {
    
    if(output.peek()==null) {
        return 0;
    }
    return output.poll();
    
}

/** Get the top element. */
public int top() {
    
    if(output.peek()==null) {
        return 0;
    }
    return output.peek();
}

/** Returns whether the stack is empty. */
public boolean empty() {
    return output.peek()==null;
}

}


作者:deep-dark
链接:https://leetcode-cn.com/problems/two-sum/solution/shuang-dui-lie-shi-xian-zhan-by-deep-dark/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值