public class Queue {
public Queue(){
_s1 = new Stack<int>();
_s2 = new Stack<int>();
}
private Stack<int> _s1 ;
private Stack<int> _s2;
// Push element x to the back of queue.
public void Push(int x) {
_s2.Push(x);
}
// Removes the element from front of queue.
public void Pop() {
while(_s2.Count > 0){
_s1.Push(_s2.Pop());
}
_s1.Pop();
while(_s1.Count > 0)
{
_s2.Push(_s1.Pop());
}
}
// Get the front element.
public int Peek() {
while(_s2.Count > 0){
_s1.Push(_s2.Pop());
}
var n = _s1.Peek();
while(_s1.Count > 0){
_s2.Push(_s1.Pop());
}
return n;
}
// Return whether the queue is empty.
public bool Empty() {
return _s2.Count == 0;
}
}版权声明:本文为博主原创文章,未经博主允许不得转载。
LeetCode -- Implement Queue using Stacks
原文:http://blog.csdn.net/lan_liang/article/details/48379771