力扣225.用队列实现栈

题目描述

使用队列实现栈的下列操作:

  • push(x) -- 元素 x 入栈
  • pop() -- 移除栈顶元素
  • top() -- 获取栈顶元素
  • empty() -- 返回栈是否为空

    注意:

+你只能使用队列的基本操作-- 也就是?push to back, peek/pop from front, size, 和?is empty?这些操作是合法的。

  • 你所使用的语言也许不支持队列。?你可以使用 list 或者 deque(双端队列)来模拟一个队列?, 只要是标准的队列操作即可。
  • 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/implement-stack-using-queues

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

代码

class MyStack {
public:
    /** Initialize your data structure here. */
    MyStack() {

    }

    /** Push element x onto stack. */
    void push(int x) {
        que.push(x);
        for (int i = 0; i + 1 < que.size(); i++) {
            que.push(que.front());
            que.pop();
        }
    }

    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int val = top();
        que.pop();
        return val;
    }

    /** Get the top element. */
    int top() {
        return que.front();
    }

    /** Returns whether the stack is empty. */
    bool empty() {
        return que.empty();
    }

private:
    queue<int> que;
};

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

结果 双百!

飘了飘了~~~

原文地址:https://www.cnblogs.com/coding365/p/12652548.html

时间: 2024-08-05 03:36:23

力扣225.用队列实现栈的相关文章

leetccode 225. 用队列实现栈(两个队列模拟)

使用队列实现栈的下列操作: push(x) -- 元素 x 入栈pop() -- 移除栈顶元素top() -- 获取栈顶元素empty() -- 返回栈是否为空注意: 你只能使用队列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合法的.你所使用的语言也许不支持队列. 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可.你可以假设所有操作都是有效的(例如, 对一个

[LeetCode] 225. 用队列实现栈

1.单纯用list就可以实现,但并未用到队列相关知识. class MyStack: def __init__(self): """ Initialize your data structure here. """ self.stack = [] def push(self, x): """ Push element x onto stack. :type x: int :rtype: void "&quo

LeetCode——225. 用队列实现栈

使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合法的. 你所使用的语言也许不支持队列. 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可. 你可以假设所有操作都是有效的(例

225. 用队列实现栈

描述 使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作-- 也就是?push to back, peek/pop from front, size, 和?is empty?这些操作是合法的. 你所使用的语言也许不支持队列.?你可以使用 list 或者 deque(双端队列)来模拟一个队列?, 只要是标准的队列操作即可. 你可以假设所有操作都是有效

225 Implement Stack using Queues(用队列实现栈)

题目意思:用队列实现栈,push(),pop(),top(),empty() 思路:用两个queue,pop时将一个queue的元素pop再push到另一个队列,queue只留最后一个元素,并pop,再将目标队列变为另一个 ps:用栈实现队列,参考剑指offer 1 class Stack { 2 private: 3 queue<int> q[2]; 4 int flag=0; 5 public: 6 // Push element x onto stack. 7 void push(int

leetcode 225. Implement Stack using Queues 利用队列构建栈 ---------- java

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You must use only s

JS数据结构第六篇 --- 二叉树力扣练习题

1.第226题:翻转二叉树 递归+迭代两种实现方式: /** 反转二叉树 * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} root * @return {TreeNode} * 第一种方式迭代 * 执行用时 :72 ms, 在所有 JavaScript 提

[力扣]144_二叉树的前序遍历

/* 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/binary-tree-preorder-traversal 著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处. */ 方法一:常规递归方式,用C语言实现(根左右) 代码实现: /** * Note

编程题目: 两个队列实现栈(Python)

感觉两个队列实现栈 比 两个栈实现队列 麻烦 1.栈为空:当两个队列都为空的时候,栈为空 2.入栈操作:当队列2为空的时候,将元素入队到队列1:当队列1位空的时候,将元素入队到队列2: 如果队列1 和 队列2 都为空的时候,那就选择入队到队列1. 3.出队操作:当两个队列都为空的时候,引发错误"栈为空": 当队列2位空的时候,如果队列1中只有一个元素,则直接将队列1中的元素出队: 如果队列1不止一个元素的时候,就将队列1的元素出队然后入队到队列2,知道队列1中只有一个元素,然后将队列1