标签:
Implement the following operations of a stack using queues.
Notes:
push to back
, peek/pop from front
, size
, and is empty
operations are valid.
题目大意:用队列,实现栈。
class MyStack { List<Integer> stack = new ArrayList<>(); // Push element x onto stack. public void push(int x) { stack.add(x); } // Removes the element on top of the stack. public void pop() { if(!empty()){ stack.remove(stack.size()-1); } } // Get the top element. public int top() { if(!empty()){ return stack.get(stack.size()-1); } return -1; } // Return whether the stack is empty. public boolean empty() { return stack.size()==0; } }
Implement Stack using Queues ——LeetCode
标签:
原文地址:http://www.cnblogs.com/aboutblank/p/4599576.html