Day22:用两个栈实现队列

272 阅读1分钟

描述

用两个栈来实现一个队列,使用n个元素来完成 n 次在队列尾部插入整数(push)和n次在队列头部删除整数(pop)的功能。 队列中的元素为int类型。保证操作合法,即保证pop操作时队列内已有元素。

数据范围: n\le1000n≤1000

要求:存储n个元素的空间复杂度为 O(n)O(n) ,插入与删除的时间复杂度都是 O(1)O(1)

示例1

输入:

["PSH1","PSH2","POP","POP"]

返回值:

1,2

说明:

"PSH1":代表将1插入队列尾部
"PSH2":代表将2插入队列尾部
"POP“:代表删除一个元素,先进先出=>返回1
"POP“:代表删除一个元素,先进先出=>返回2   

示例2

输入:

["PSH2","POP","PSH1","POP"]

返回值:

2,1

思路:

1、当插入时,直接插入 stack1

2、当弹出时,当 stack2 不为空,弹出 stack2 栈顶元素,如果 stack2 为空,将 stack1 中的全部数逐个出栈入栈 stack2,再弹出 stack2 栈顶元素

Java:

Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();

public void push(int node) {
    stack1.push(node);

}

public int pop() {
    if (stack2.empty()) {
        while (!stack1.empty()) {
            stack2.push(stack1.pop());
        }
    }
    return stack2.pop();
}

c++:

#include <iostream>
#include <string>
using namespace std;
#include <algorithm>
#include <stack>
class Solution {
public:
    void push(int node) {
        stack1.push(node);
    }

    int pop() {
        if( stack2.empty( )) {
            while( !stack1.empty( )){
                stack2.push(stack1.top());
                stack1.pop();
            }
        }
        int ans = stack2.top();
        stack2.pop();
        return ans;
    }

private:
    stack<int> stack1;
    stack<int> stack2;
};
  • 入栈的时间复杂度为O(1),因为只要进行一个push操作就行了,但是出栈的时间复杂度为时间复杂度为O(n),因为当我们的stack2为空的时候,需要将stack1的所有的结点都进行一个出栈。
  • 需要开stack存储暂时入栈的所有的数字,空间复杂度为O(n)