-
Notifications
You must be signed in to change notification settings - Fork 13
/
MyQueue232.java
77 lines (74 loc) · 1.62 KB
/
MyQueue232.java
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
74
75
76
77
package Leetcode;
import java.util.LinkedList;
import java.util.Queue;
class MyQueue232 {
/** Method1: two stacks; Time: pop() / peek() - O(N), others-O(1); Space:O(N);**/
/**
* Stack的方法: offer, poll, peek
*/
private Queue<Integer> stack1;
private Queue<Integer> stack2;
public MyQueue232() {
stack1 = new LinkedList<>();
stack2 = new LinkedList<>();
}
public void push(int x) {
stack1.offer(x);
}
public int pop() {
while(!stack1.isEmpty()){
stack2.offer(stack1.poll());
}
int res = stack2.poll();
while(!stack2.isEmpty()){
stack1.offer(stack2.poll());
}
return res;
}
public int peek() {
while(!stack1.isEmpty()){
stack2.offer(stack1.poll());
}
int res = stack2.peek();
while(!stack2.isEmpty()){
stack1.offer(stack2.poll());
}
return res;
}
public boolean empty() {
return stack1.isEmpty();
}
}
class MyQueue {
/**Method2: add a front; Time: pop()-O(N), others-O(1); Space:all methods-O(N);**/
private Queue<Integer> stack1;
private Queue<Integer> stack2;
private int front;
public MyQueue() {
stack1 = new LinkedList<>();
stack2 = new LinkedList<>();
}
public void push(int x) {
if(stack1.isEmpty()){
front = x;
}
stack1.offer(x);
}
public int pop() {
if(stack2.isEmpty()){
while(!stack1.isEmpty()){
stack2.offer(stack1.poll());
}
}
return stack2.poll();
}
public int peek() {
while(stack2.isEmpty()){
return front;
}
return stack2.peek();
}
public boolean empty() {
return stack1.isEmpty() && stack2.isEmpty();
}
}