算法60天训练–9-15-day10

本文介绍了如何使用栈和队列数据结构在Java和C++中实现队列功能,包括用两个栈模拟队列和一个队列模拟栈的原理、代码实现以及它们的时间和空间复杂度分析。
摘要由CSDN通过智能技术生成

232. 用栈实现队列

思路

首先应该先明确队列是先进先出,

而栈是先进后出,而如果想用栈实现队列,就可以尝试用两个栈

进栈和出栈

  • 进栈模拟入队列
  • 出栈模拟先出队列

画图如下

image-20230917183021976

Code

Java

class MyQueue {
				
				Stack<Integer> stIn;
				Stack<Integer> stOut;
    public MyQueue() {
      stIn = new Stack<>();
								stOut = new Stack<>();
								
    }
    
    public void push(int x) {
						stIn.push(x);
    }
    
    public int pop() {
       
						if(stOut.isEmpty()){
						    while(!stIn.isEmpty()){
						        stOut.push(stIn.pop());
						    } 
						}
						return stOut.pop();
    }
    
    public int peek() {
							int res = this.pop();
							stOut.push(res);
							return res;
							
    }
    
    public boolean empty() {
							return stOut.isEmpty()&&stIn.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();
 */

C++

class MyQueue {
public:
				stack<int>	 stIn;
				stack<int> stOut;
				
    MyQueue() {

    }
    
    void push(int x) {
						stIn.push(x);
    }
    
    int pop() {
							if(stOut.empty()){
							    while(!stIn.empty()){
							        stOut.push(stIn.top());
							        stIn.pop();
							    }
							}
							int result = stOut.top();
							stOut.pop();
							return result;
    }
    
    int peek() {
						int res = this->pop();
						stOut.push(res);
						return res;
    }
    
     bool empty() {
						return stIn.empty()&&stOut.empty();
    }
};

/**
 * 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();
 * bool param_4 = obj.empty();
 */

复杂度

时间复杂度: push和empty为O(1), pop和peek为O(n)

空间复杂度: O(n)

225. 用队列实现栈

思路

队列是先进先出原则,

而栈是先进后出原则

因此,可以使用两个队列来实现栈

可以使用一个队列来实现栈

满足先进后出的方法就是; 入队列之后,就将这个数放到队首

image-20230917184757095

Code

C++

class MyStack {
public:	
				queue<int> que;
    MyStack() {

    }
    
    void push(int x) {
						que.push(x);
    }
    
    int pop() {
						int size = que.size();
						size--;
						while(size--){
						    que.push(que.front());
						    que.pop();
						}
						int res = que.front();
						que.pop();
						return res;
    }
    
    int top() {
					return que.back();
    }
    
    bool empty() {
						return que.empty();
    }
};

/**
 * 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();
 * bool param_4 = obj->empty();
 */

Java

class MyStack {
    Queue<Integer> que = new LinkedList<>();
    public MyStack() {

    }
    
    public void push(int x) {
            que.add(x);
    }
    
    public int pop() {
        rePosition();
        return que.poll();
    }
    
    public int top() {
        rePosition();
        int res = que.poll();
        que.add(res);
        return res;
    }
    
    public boolean empty() {
        return que.isEmpty();
    }
    public void rePosition(){
        int size = que.size();
        size--;
        while(size-- > 0){
            que.add(que.poll());
        }
    }
}

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

复杂度

  • 时间复杂度: pop为O(n),其他为O(1)
  • 空间复杂度: O(n)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落雨既然

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值