-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode232.java
More file actions
58 lines (51 loc) · 1.38 KB
/
LeetCode232.java
File metadata and controls
58 lines (51 loc) · 1.38 KB
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
import java.util.Deque;
import java.util.LinkedList;
public class LeetCode232 {
public static void main(String[] args) {
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
System.out.println(myQueue.peek()); // return 1
System.out.println(myQueue.pop()); // return 1, queue is [2]
System.out.println(myQueue.empty()); // return false
}
}
class MyQueue {
// NOTE: 时间复杂度略高
Deque<Integer> s1;
Deque<Integer> s2;
boolean state;
public MyQueue() {
s1 = new LinkedList<Integer>();
s2 = new LinkedList<Integer>();
state = true;
}
public void push(int x) {
s1.push(x);
}
public int pop() {
int size = s1.size();
for (int i = 0; i < size - 1; i++) {
s2.push(s1.pop());
}
int res = s1.pop();
for (int i = 0; i < size - 1; i++) {
s1.push(s2.pop());
}
return res;
}
public int peek() {
int size = s1.size();
for (int i = 0; i < size - 1; i++) {
s2.push(s1.pop());
}
int res = s1.peek();
for (int i = 0; i < size - 1; i++) {
s1.push(s2.pop());
}
return res;
}
public boolean empty() {
return s1.isEmpty();
}
}