leetcode 225 用列队实现栈

简介: leetcode 225 用列队实现栈

用列队实现栈


caff3bcb268e45e58d1f411818764c46.png

两个队列实现栈

#include <iostream>
#include<string>
#include<vector>
#include<unordered_map>
#include <algorithm>
#include<map>
#include<stack>
#include <queue>
using namespace std;
class MyStack {
public:
    MyStack() {
    }
    void push(int x) {
        que_1.push(x);
    }
    int pop() {
        int result = 0;
        while (que_1.empty() == 0)
        {
            result = que_1.front();
            que_2.push(que_1.front());
            que_1.pop();
        }
        int num = que_2.size() - 1;
        for (int i = 0; i < num ; i++)
        {
            que_1.push(que_2.front());
            que_2.pop();
        }
        que_2.pop();
        return result;
    }
    int top() {
        int result = 0;
        while (que_1.empty() == 0)
        {
            result = que_1.front();
            que_2.push(que_1.front());
            que_1.pop();
        }
        while (que_2.empty() == 0)
        {
            que_1.push(que_2.front());
            que_2.pop();
        }
        return result;
    }
    bool empty() {
        if (que_1.empty() == 1)return 1;
        else return 0;
    }
public:
    queue<int> que_1;
    queue<int> que_2;
};
int main() {
     MyStack obj;
     obj.push(1);
     obj.push(2);
     obj.push(3);
     cout << obj.pop() << endl;
     cout << obj.pop() << endl;
     cout << obj.pop() << endl;
     cout << obj.empty() << endl;
     return 0;
}


一个队列实现栈


class MyStack {
public:
    MyStack() {
    }
    void push(int x) {
        que_1.push(x);
    }
    int pop() {
        int result = 0;
        int size  = que_1.size() -1 ;
        for (int i = 0; i < size; i++)
        {
            result = que_1.front();
            que_1.pop();
            que_1.push(result);
        }
        result = que_1.front();
        que_1.pop();
        return result;
    }
    int top() {
        int result = 0;
        int size = que_1.size() ;
        for (int i = 0; i < size; i++)
        {
            result = que_1.front();
            que_1.pop();
            que_1.push(result);
        }
        return result;
    }
    bool empty() {
        if (que_1.empty() == 1)return 1;
        else return 0;
    }
public:
    queue<int> que_1;
};

二刷

一个队列实现

class MyStack {
public:
    MyStack() {
    }
    void push(int x) {
        q1.push(x);
    }
    int pop() {
        int num = q1.size()-1;
        while(num--)
        {
            int tmp = q1.front();
            q1.pop();
            q1.push(tmp);
        }
        int result = q1.front();
        q1.pop();
        return result;
    }
    int top() {
        return q1.back();
    }
    bool empty() {
        return q1.empty();
    }
public:
    queue<int> q1;
};
/**
 * 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();
 */
相关文章
|
4月前
|
Go C++
【力扣】2696. 删除子串后的字符串最小长度(模拟 栈 C++ Go实现栈)
【2月更文挑战第18天】2696. 删除子串后的字符串最小长度(模拟 栈 C++ Go实现栈)
52 6
|
4月前
LeetCode 热题100——单调栈
LeetCode 热题100——单调栈
41 0
|
2月前
|
存储 算法 测试技术
力扣经典150题第五十四题:最小栈
力扣经典150题第五十四题:最小栈
27 0
|
3月前
|
存储 算法 索引
力扣每日一题 6/24 模拟 数组 单调栈
力扣每日一题 6/24 模拟 数组 单调栈
26 0
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 30. 包含min函数的栈
本文提供了实现一个包含min函数的栈的Python代码,确保min、push和pop操作的时间复杂度为O(1)。
17 4
|
1月前
|
Python
【Leetcode刷题Python】946. 验证栈序列
LeetCode题目“946. 验证栈序列”的Python解决方案,通过模拟栈的压入和弹出操作来验证给定的两个序列是否能通过合法的栈操作得到。
20 6
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 09. 用两个栈实现队列
使用两个栈实现队列的Python解决方案,包括初始化两个栈、实现在队列尾部添加整数的appendTail方法和在队列头部删除整数的deleteHead方法,以及相应的示例操作。
31 2
|
1月前
|
Python
【Leetcode刷题Python】232. 用栈实现队列
如何使用Python语言通过两个栈来实现队列的所有基本操作,包括入队(push)、出队(pop)、查看队首元素(peek)和判断队列是否为空(empty),并提供了相应的代码实现。
14 0
|
2月前
|
Python
155. 最小栈 力扣 python 空间换时间 o(1) 腾讯面试题
155. 最小栈 力扣 python 空间换时间 o(1) 腾讯面试题
|
3月前
|
存储 算法 Python
二刷力扣--栈和队列
二刷力扣--栈和队列