首页 > 代码库 > leetcode232
leetcode232
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。