0%

栈和队列设计

225.用队列实现栈

题目描述

使用队列实现栈的下列操作:

  • push(x) — 元素 x 入栈
  • pop() — 移除栈顶元素
  • top() — 获取栈顶元素
  • empty() — 返回栈是否为空

注意:

  • 你只能使用队列的基本操作— 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合法的。
  • 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
  • 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-stack-using-queues
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

代码

两个队列, 压入 - O(n)O(n), 弹出 - O(1)O(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
45
46
47
48
49
50
class MyStack {
private Queue<Integer> q1 = new LinkedList<>();
private Queue<Integer> q2 = new LinkedList<>();
private int top;
/** Initialize your data structure here. */
public MyStack() {

}

/** Push element x onto stack. */
public void push(int x) {
q2.add(x);
top = x;
while (!q1.isEmpty()) {
q2.add(q1.remove());
}
Queue<Integer> temp = q1;
q1 = q2;
q2 = temp;
}

/** Removes the element on top of the stack and returns that element. */
public int pop() {
q1.remove();
int res = top;
if (!q1.isEmpty()) {
top = q1.peek();
}
return res;
}

/** Get the top element. */
public int top() {
return top;
}

/** Returns whether the stack is empty. */
public boolean empty() {
return q1.isEmpty();
}
}

/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/

一个队列, 压入 - O(n)O(n), 弹出 - O(1)O(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
class MyStack {
private LinkedList<Integer> q1 = new LinkedList<>();

// Push element x onto stack.
public void push(int x) {
q1.add(x);
int sz = q1.size();
while (sz > 1) {
q1.add(q1.remove());
sz--;
}
}

// Removes the element on top of the stack.
public int pop() {
return q1.remove();
}

// Return whether the stack is empty.
public boolean empty() {
return q1.isEmpty();
}

// Get the top element.
public int top() {
return q1.peek();
}


}

232.用栈实现队列

题目描述

使用栈实现队列的下列操作:

  • push(x) — 将一个元素放入队列的尾部。

  • pop() — 从队列首部移除元素。

  • peek() — 返回队列首部的元素。

  • empty() — 返回队列是否为空。

示例:

1
2
3
4
5
6
7
MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);
queue.peek(); // 返回 1
queue.pop(); // 返回 1
queue.empty(); // 返回 false

说明:

  • 你只能使用标准的栈操作 — 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的
  • 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
  • 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-queue-using-stacks
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

代码

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
45
46
47
48
49
50
class MyQueue {

Stack<Integer> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
int top;

/** Initialize your data structure here. */
public MyQueue() {

}

/** Push element x to the back of queue. */
public void push(int x) {
if (stack1.empty())
top = x;
while (!stack1.isEmpty())
stack2.push(stack1.pop());
stack2.push(x);
while (!stack2.isEmpty())
stack1.push(stack2.pop());

}

/** Removes the element from in front of queue and returns that element. */
public int pop() {
int res = stack1.pop();
if (!stack1.empty())
top = stack1.peek();
return res;
}

/** Get the front element. */
public int peek() {
return top;
}

/** Returns whether the queue is empty. */
public boolean empty() {
return stack1.isEmpty();
}
}

/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/

比较好理解的版本

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.Stack;

public class MyQueue {

private Stack<Integer> stackPush;
private Stack<Integer> stackPop;

/**
* Initialize your data structure here.
*/
public MyQueue() {
stackPush = new Stack<>();
stackPop = new Stack<>();
}

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

/**
* 辅助方法:一次性将 stackPush 里的所有元素倒入 stackPop
* 注意:1、该操作只在 stackPop 里为空的时候才操作,否则会破坏出队入队的顺序
* 2、在 peek 和 pop 操作之前调用该方法
*/
private void shift() {
if (stackPop.isEmpty()) {
while (!stackPush.isEmpty()) {
stackPop.push(stackPush.pop());
}
}
}

/**
* Removes the element from in front of queue and returns that element.
*/
public int pop() {
shift();
if (!stackPop.isEmpty()) {
return stackPop.pop();
}
throw new RuntimeException("队列里没有元素");
}

/**
* Get the front element.
*/
public int peek() {
shift();
if (!stackPop.isEmpty()) {
return stackPop.peek();
}
throw new RuntimeException("队列里没有元素");
}

/**
* Returns whether the queue is empty.
*/
public boolean empty() {
return stackPush.isEmpty() && stackPop.isEmpty();
}
}

/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/