您的位置:首页 > 博客中心 > 互联网 >

leetcode232

时间:2022-05-01 23:40

public class MyQueue
    {
        Stack S = new Stack();
        /** Initialize your data structure here. */
        public MyQueue()
        {

        }

        /** Push element x to the back of queue. */
        public void Push(int x)
        {
            S.Push(x);
        }

        /** Removes the element from in front of queue and returns that element. */
        public int Pop()
        {
            List list = new List();
            var x = -1;
            while (S.Count > 0)
            {
                x = S.Pop();
                list.Add(x);
            }

            for (int i = list.Count - 2; i >= 0; i--)
            {
                S.Push(list[i]);
            }

            return x;
        }

        /** Get the front element. */
        public int Peek()
        {
            if (S.Count > 0)
            {
                return S.ElementAt(S.Count - 1);
            }
            else
            {
                return -1;
            }
        }

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

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

本类排行

今日推荐

热门手游