day10 算法打卡| 栈与队列|Leetcode232用栈实现队列、225用栈实现队列 | 理解队列与栈的不同

Leetcode232:用栈实现队列

题目链接:https://leetcode.cn/problems/implement-queue-using-stacks/description/

思路:用两个栈实现队列操作

Java实现代码:(栈可以使用Stack或Deque实现)

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;

//leetcode submit region begin(Prohibit modification and deletion)
class MyQueue {
    //使用stack可以
    private Deque<Integer> inStack;
    private Deque<Integer> outStack;

    public MyQueue() {
        inStack = new ArrayDeque<>();
        outStack = new ArrayDeque<>();
    }
    
    public void push(int x) {
        inStack.push(x);
    }
    
    public int pop() {
        if(outStack.isEmpty()){
            while (!inStack.isEmpty()){
                outStack.push(inStack.pop());
            }
        }
        int popnum = outStack.pop();
        return popnum;
    }
    
    public int peek() {
        int peeknum = pop();
        outStack.push(peeknum);
        return peeknum;
    }
    
    public boolean empty() {
        if(outStack.isEmpty()&& inStack.isEmpty()){
            return true;
        }
        return false;
    }
}

/**
 * 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();
 */
//leetcode submit region end(Prohibit modification and deletion)

Leetcode225:用栈实现队列

(队列可以使用Queue或Deque实现)

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

可以使用两个队列实现,也可以使用一个队列实现如下:

使用一个队列Queue实现的Java代码:

import java.util.LinkedList;
import java.util.Queue;

//leetcode submit region begin(Prohibit modification and deletion)
class MyStack {
    private Queue<Integer> thequeue;

    public MyStack() {
        thequeue = new LinkedList<>();
    }
    
    public void push(int x) {
        thequeue.add(x);
    }
    public int pop() {
        int size = thequeue.size();
        for(int i =0;i<size-1;i++){
            thequeue.add(thequeue.poll());
        }
        return thequeue.poll();
    }
    
    public int top() {
        int thenum = pop();
        push(thenum);
        return thenum;
    }
    
    public boolean empty() {
        return thequeue.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();
 */
//leetcode submit region end(Prohibit modification and deletion)

使用两个队列Queue实现的Java代码:(两个队列实现的方法有很多,这是其中一种)

import java.util.LinkedList;
import java.util.Queue;

//leetcode submit region begin(Prohibit modification and deletion)
class MyStack {
    private Queue<Integer> thequeue1;
    private Queue<Integer> thequeue2;

    public MyStack() {
        thequeue1 = new LinkedList<>();
        thequeue2 = new LinkedList<>();
    }
    
    public void push(int x) {
        if(thequeue1.isEmpty()){
            thequeue2.add(x);
        }else {
            thequeue1.add(x);
        }
    }
    public int pop() {
        Queue<Integer> pushqueue = new LinkedList<>();
        Queue<Integer> popqueue = new LinkedList<>();
        if(thequeue1.isEmpty()){
            pushqueue = thequeue1;
            popqueue = thequeue2;
        }else {
            pushqueue = thequeue2;
            popqueue = thequeue1;
        }
        //从pop中输出放入push (出pop.size-1个)
        int size = popqueue.size();
        for(int i=0;i<size-1;i++){
            pushqueue.add(popqueue.poll());
        }
        return popqueue.poll();
    }
    
    public int top() {
        int thenum = pop();
        push(thenum);
        return thenum;
    }
    
    public boolean empty() {
        if (thequeue1.isEmpty()&&thequeue2.isEmpty()){
            return true;
        }else {
            return false;
        }
    }
}

/**
 * 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();
 */
//leetcode submit region end(Prohibit modification and deletion)

  • 17
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值