一、题目
用栈实现队列
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
- void push(int x) 将元素 x 推到队列的末尾
- int pop() 从队列的开头移除并返回元素
- int peek() 返回队列开头的元素
- boolean empty() 如果队列为空,返回 true ;否则,返回 false
说明:
- 你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is
empty 操作是合法的。 - 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
示例 1:
输入:
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2],[], [], []]
输出:
[null, null, null, 1, 1, false]
解释:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
提示:
1 <= x <= 9
最多调用 100 次 push、pop、peek 和 empty 假设所有操作都是有效的(例如,一个空的队列不会调用 pop 或者 peek 操作)
用队列实现栈
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:
- void push(int x) 将元素 x 压入栈顶。
- int pop() 移除并返回栈顶元素。
- int top() 返回栈顶元素。
- boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
示例:
输入:
["MyStack", "push", "push", "top", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 2, 2, false]
解释:
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // 返回 2
myStack.pop(); // 返回 2
myStack.empty(); // 返回 False
二、解题思路
使用两个堆栈实现队列
使用两个堆栈,一个存堆栈用于存储 push 进队列的数据,一个取堆栈用于 pop/peek 出队列的数据。每次 push 时直接写入存堆栈, pop/peek 前先判断取堆栈内是否存在数据,若不存在数据则将存堆栈内的数据全部移动到取堆栈内。
使用两个栈实现队列
和用栈实现队列的思路一致,用一个队列存储数据,每次push操作时都往同一个栈内存数据;每次pop/top操作时,先将数据全部移动到另外一个队列中,记录最后一个数据,然后将原队列复原,将最后一个数据返回即可。
三、代码实现
用栈实现队列
golang代码实现如下:
type MyQueue struct {
// 建立两个 stack 一个用于存储字符串,一个用于查询和出栈,模拟队列功能
storeStack []int
queryStack []int
}
func Constructor() MyQueue {
return MyQueue{
storeStack: []int{},
queryStack: []int{},
}
}
func (this *MyQueue) Push(x int) {
this.storeStack = append(this.storeStack, x)
}
func (this *MyQueue) Pop() int {
if len(this.queryStack) == 0 {
this.exchange()
}
i := this.queryStack[len(this.queryStack)-1]
this.queryStack = this.queryStack[:len(this.queryStack)-1]
return i
}
func (this *MyQueue) exchange() {
for len(this.storeStack) > 0 {
this.queryStack = append(this.queryStack, this.storeStack[len(this.storeStack)-1])
this.storeStack = this.storeStack[:len(this.storeStack)-1]
}
}
func (this *MyQueue) Peek() int {
if len(this.queryStack) == 0 {
this.exchange()
}
return this.queryStack[len(this.queryStack)-1]
}
func (this *MyQueue) Empty() bool {
return len(this.storeStack) == 0 && len(this.queryStack) == 0
}
/**
* Your MyQueue object will be instantiated and called as such:
* obj := Constructor();
* obj.Push(x);
* param_2 := obj.Pop();
* param_3 := obj.Peek();
* param_4 := obj.Empty();
*/
解题效果
用队列实现栈
golang 代码实现
type MyStack struct {
queue []int
}
func Constructor() MyStack {
return MyStack{
queue: []int{},
}
}
func (this *MyStack) Push(x int) {
this.queue = append(this.queue, x)
}
func (this *MyStack) Pop() int {
tmp := make([]int, 0, len(this.queue))
for len(this.queue) > 1 {
tmp = append(tmp, this.queue[0])
this.queue = this.queue[1:]
}
answer := this.queue[0]
this.queue = tmp
return answer
}
func (this *MyStack) Top() int {
tmp := make([]int, 0, len(this.queue))
for len(this.queue) > 1 {
tmp = append(tmp, this.queue[0])
this.queue = this.queue[1:]
}
answer := this.queue[0]
tmp = append(tmp, answer)
this.queue = tmp
return answer
}
func (this *MyStack) Empty() bool {
return len(this.queue) == 0
}
解题结果: