力扣-232. 用栈实现队列

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(pushpoppeekempty):

实现 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
输入:
["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

解题思路

需要两个栈,一个输入栈,一个输出栈。在push数据的时候只需将数据放进输入栈。在pop数据时,若输出栈为空,就把输入栈中的数据全部弹出导入,再从输出栈弹出数据。如果输出栈不为空,则直接从输出栈弹出数据即可。如果输入栈和输出栈都为空的话,说明模拟的队列为空了。

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var MyQueue = function() {
this.in = []
this.out = []
};

/**
* @param {number} x
* @return {void}
*/
MyQueue.prototype.push = function(x) {
this.in.push(x)
};

/**
* @return {number}
*/
MyQueue.prototype.pop = function() {
if(!this.out.length){
while(this.in.length){
this.out.push(this.in.pop())
}
}
return this.out.pop()
};

/**
* @return {number}
*/
MyQueue.prototype.peek = function() {
let peekNum = this.pop()
this.out.push(peekNum)
return peekNum
};

/**
* @return {boolean}
*/
MyQueue.prototype.empty = function() {
return !this.in.length && !this.out.length
};

力扣-225. 用队列实现栈

请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(pushtoppopempty)。

实现 MyStack 类:

  • void push(int x) 将元素 x 压入栈顶。
  • int pop() 移除并返回栈顶元素。
  • int top() 返回栈顶元素。
  • boolean empty() 如果栈是空的,返回 true ;否则,返回 false

注意:

  • 你只能使用队列的基本操作 —— 也就是 push to backpeek/pop from frontsizeis empty 这些操作。
  • 你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
输入:
["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

解题思路

使用两个队列:队列1用于入栈,队列2用于备份队列1最后一个元素以外的元素。当需要出栈的时候,队列1中最后一个元素以外的元素备份到队列2中(使用一个队列:也可以直接将队列1中最后一个元素以外的元素shift后push到队列1中),接下来弹出(pop)队列1中最后的元素,将队列2中备份的元素重新存储到队列1.

代码实现

使用两个队列
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
var MyStack = function() {
this.queue1 = []
this.queue2 = []
};

/**
* @param {number} x
* @return {void}
*/
MyStack.prototype.push = function(x) {
this.queue1.push(x)
};

/**
* @return {number}
*/
MyStack.prototype.pop = function() {
let length = this.queue1.length-1
while(length--){
this.queue2.push(this.queue1.shift())
}
let popNum = this.queue1.pop()
let len = this.queue2.length
while(len--){
this.queue1.push(this.queue2.shift())
}
return popNum
};

/**
* @return {number}
*/
MyStack.prototype.top = function() {
let topNum = this.pop()
this.queue1.push(topNum)
return topNum
};

/**
* @return {boolean}
*/
MyStack.prototype.empty = function() {
return !this.queue1.length && !this.queue2.length
};
使用一个队列
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var MyStack = function() {
this.queue = [];
};

/**
* Push element x onto stack.
* @param {number} x
* @return {void}
*/
MyStack.prototype.push = function(x) {
this.queue.push(x);
};

/**
* Removes the element on top of the stack and returns that element.
* @return {number}
*/
MyStack.prototype.pop = function() {
let size = this.queue.length;
while(size-- > 1) {
this.queue.push(this.queue.shift());
}
return this.queue.shift();
};

/**
* Get the top element.
* @return {number}
*/
MyStack.prototype.top = function() {
const x = this.pop();
this.queue.push(x);
return x;
};

/**
* Returns whether the stack is empty.
* @return {boolean}
*/
MyStack.prototype.empty = function() {
return !this.queue.length;
};