码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode232

时间:2017-04-22 10:40:23      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:data   blank   imp   queue   ==   tps   https   ble   com   

public class MyQueue
    {
        Stack<int> S = new Stack<int>();
        /** 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<int> list = new List<int>();
            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();
     */

https://leetcode.com/problems/implement-queue-using-stacks/#/description

leetcode232

标签:data   blank   imp   queue   ==   tps   https   ble   com   

原文地址:http://www.cnblogs.com/asenyang/p/6746969.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!