leetcode225

public class MyStack
    {
        Queue<int> Q = new Queue<int>();

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

        }

        /** Push element x onto stack. */
        public void Push(int x)
        {
            Q.Enqueue(x);
        }

        /** Removes the element on top of the stack and returns that element. */
        public int Pop()
        {
            var tempQ = new Queue<int>();

            var pop = -1;

            while (Q.Count > 0)
            {
                var cur = Q.Dequeue();

                if (Q.Count == 0)
                {
                    pop = cur;
                }
                else
                {
                    tempQ.Enqueue(cur);
                }
            }

            while (tempQ.Count > 0)
            {
                var cur = tempQ.Dequeue();
                Q.Enqueue(cur);
            }

            return pop;
        }

        /** Get the top element. */
        public int Top()
        {
            var tempQ = new Queue<int>();

            var pop = -1;

            while (Q.Count > 0)
            {
                var cur = Q.Dequeue();
                if (Q.Count == 0)
                {
                    pop = cur;
                }

                tempQ.Enqueue(cur);
            }

            while (tempQ.Count > 0)
            {
                var cur = tempQ.Dequeue();
                Q.Enqueue(cur);
            }

            return pop;
        }

        /** Returns whether the stack is empty. */
        public bool Empty()
        {
            return Q.Count == 0;
        }
    }

/**
 * 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://leetcode.com/problems/implement-stack-using-queues/#/description

时间: 2024-07-29 01:13:30

leetcode225的相关文章

LeetCode225:Implement Stack using Queues

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 stand

Leetcode225 用栈实现队列

大众思路: 用两个栈实现,记为s1,s2 1.元素入栈时,加入s1 2.元素出栈时,对s2进行判断,如果s2为空,则将全部s1元素弹出并压入到s2,然后从s2栈顶弹出一个元素:如果s2不为空,则直接从s2的栈顶弹出一个元素 冷门思路: 这种思路效率比较低 1.元素入栈时,加入s1 2.元素出栈时,对s2进行判断,如果s2为空,则将全部s1元素弹出并压入到s2,然后从s2栈顶弹出一个元素:再将元素倒回s1 这样做的缺点是需要在两个栈之间倒来倒去,效率较低. 如下图所示: 我的思路是第一种,C++实

LeetCode225——Implement Stack using Queues

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

LeetCode225 Implemet 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 on

LeetCode225 栈&#183;用队列实现栈(C++)

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