您的位置:首页 > 产品设计 > UI/UE

leetcode-Implement Queue using Stacks

2015-11-04 22:24 465 查看
懒地打字。

class MyQueue
{
Stack<Integer> s;
Stack<Integer> t;

public MyQueue()
{
s=new Stack<Integer>();
t=new Stack<Integer>();
}

// Push element x to the back of queue.
public void push(int x) {
s.push(x);
}

// Removes the element from in front of queue.
public void pop() {
int len=s.size();
for(int i=0;i<len;i++)
{
t.push(s.pop());
}
t.pop();
int slen=t.size();
for(int j=0;j<slen;j++)
{
s.push(t.pop());
}
}

// Get the front element.
public int peek() {
int r = 0;
int len = s.size();
for (int i = 0; i < len; i++) {
t.push(s.pop());
}
r=t.peek();
for (int i = 0; i < len; i++) {
s.push(t.pop());
}
return r;
}

// Return whether the queue is empty.
public boolean empty() {
return s.empty()&&t.empty();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: