标签:
Implement the following operations of a stack using queues.
Notes:
push to back
, peek/pop from front
, size
, and is empty
operations are valid.
Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and all test cases.
class MyStack { private Queue queue; // Push element x onto stack. public void push(int x) { Queue q = new LinkedList(); q.add(x); q.add(queue); queue = q; } // Removes the element on top of the stack. public void pop() { queue.remove(); queue = (Queue) queue.peek(); } // Get the top element. public int top() { return (int)queue.peek(); } // Return whether the stack is empty. public boolean empty() { return queue==null; } }
LeetCode-Implement Stack Using Queues
标签:
原文地址:http://www.cnblogs.com/lishiblog/p/5870428.html