标签:href pad port depend font note count move oid
Implement the following operations of a queue using stacks.
push to top, peek/pop from top, size, and is empty operations are valid.
public class MyQueue {public Stack<int> stack;public MyQueue() {stack = new Stack<int>();}public void Push(int x) {stack.Push(x);}public int Pop() {Stack<int> tempStack = new Stack<int>();int count = stack.Count();for (int i = 0; i < count; i++) {tempStack.Push(stack.Pop());}int peek = tempStack.Pop();stack.Clear();count = tempStack.Count();for (int i = 0; i < count; i++) {stack.Push(tempStack.Pop());}return peek;}public int Peek() {int[] arr = stack.ToArray();return arr[stack.Count - 1];}public bool Empty() {return this.stack.Count == 0;}}
232. 用栈实现队列 Implement Queue using Stacks
标签:href pad port depend font note count move oid
原文地址:http://www.cnblogs.com/xiejunzhao/p/6493037.html