您的位置:首页 > 其它

Leetcode 225,232 用两个队列实现栈,用两个栈实现队列

2016-01-11 14:26 381 查看

Leetcode 225,232 用两个队列实现栈,用两个栈实现队列

232 Implement Queue using Stacks My Submissions Question

Total Accepted: 29497 Total Submissions: 86968 Difficulty: Easy

Implement the following operations of a queue using stacks.

**push(x) – Push element x to the back of queue.

pop() – Removes the element from in front of queue.

peek() – Get the front element.

empty() – Return whether the queue is empty.

Notes:

You must use only standard operations of a stack – which means only push to top, peek/pop from top, size, and is empty operations are valid.

Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.

You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).**

s1,s2两个栈,出队列时如果s2为空,将s1的元素全部Pop到s2再从s2 Pop。否则直接s2 Pop。

入队列时直接s1 Push

代码

public class Queue {
Stack<int> s1 = new Stack<int>();
Stack<int> s2 = new Stack<int>();
// Push element x to the back of queue.
public void Push(int x) {
s1.Push(x);
}

// Removes the element from front of queue.
public void Pop() {
if(s2.Count==0) {
while(s1.Count>0) s2.Push(s1.Pop());
}
s2.Pop();
}

// Get the front element.
public int Peek() {
if(s2.Count==0) {
while(s1.Count>0) s2.Push(s1.Pop());
}
return s2.Peek();
}

// Return whether the queue is empty.
public bool Empty() {
return s1.Count==0 && s2.Count==0;
}
}


**225 Implement Stack using Queues My Submissions Question

Total Accepted: 27669 Total Submissions: 91210 Difficulty: Easy

Implement the following operations of a stack using queues.

push(x) – Push element x onto stack.

pop() – Removes the element on top of the stack.

top() – Get the top element.

empty() – Return whether the stack is empty.

Notes:

You must use only standard operations of a queue – which means only push to back, peek/pop from front, size, and is empty operations are valid.

Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.

You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).**

感觉这个方法比较笨,不知道有没有更好的。始终保持一个队列为空。入队列入非空的队列。出队列时把非空队列的除最后一个外全部转移到空的队列,然后出非空队列最后一个。

代码

public class Stack {
Queue q1=new Queue(),q2=new Queue();
// Push element x onto stack.
public void Push(int x) {
if(q1.Count!=0) q1.Enqueue(x);
else if(q2.Count!=0) q2.Enqueue(x);
else q1.Enqueue(x);
}

// Removes the element on top of the stack.
public void Pop() {
Queue q = q1.Count==0?q2:q1;
Queue qq = q1.Count==0?q1:q2;
while(q.Count>1) {
qq.Enqueue(q.Dequeue());
}
q.Dequeue();
}

// Get the top element.
public int Top() {
Queue q = q1.Count==0?q2:q1;
Queue qq = q1.Count==0?q1:q2;
while(q.Count>1) {
qq.Enqueue(q.Dequeue());
}
int ret = (int)q.Peek();
qq.Enqueue(q.Dequeue());
return ret;
}

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