标签:
Implement the following operations of a queue using stacks.
push to top
, peek/pop from top
, size
, and is empty
operations are valid.class MyQueue { // Push element x to the back of queue. Stack<Integer> stackA = new Stack<Integer>(); Stack<Integer> stackB = new Stack<Integer>(); public void push(int x) { stackA.push(x); } // Removes the element from in front of queue. public void pop() { if(!stackB.isEmpty()) { stackB.pop(); } else { while(!stackA.isEmpty()){ stackB.push(stackA.pop()); } stackB.pop(); } } // Get the front element. public int peek() { if(!stackB.isEmpty()) { return stackB.peek(); } else { while(!stackA.isEmpty()){ stackB.push(stackA.pop()); } return stackB.peek(); } } // Return whether the queue is empty. public boolean empty() { return (stackA.isEmpty() && stackB.isEmpty()); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
LeetCode Implement Queue using Stacks
标签:
原文地址:http://blog.csdn.net/yangliuy/article/details/46985947