-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode225.java
More file actions
57 lines (49 loc) · 1.29 KB
/
LeetCode225.java
File metadata and controls
57 lines (49 loc) · 1.29 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
import java.util.LinkedList;
import java.util.Queue;
public class LeetCode225 {
public static void main(String[] args) {
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
System.out.println(myStack.top()); // 返回 2
System.out.println(myStack.pop()); // 返回 2
System.out.println(myStack.empty()); // 返回 False
}
}
class MyStack {
Queue<Integer> q1;
Queue<Integer> q2;
public MyStack() {
this.q1 = new LinkedList<>();
this.q2 = new LinkedList<>();
}
public void push(int x) {
this.q1.offer(x);
}
public int pop() {
int size = q1.size();
for (int i = 0; i < size - 1; i++) {
this.q2.offer(q1.poll());
}
int res = this.q1.poll();
Queue<Integer> temp = this.q1;
this.q1 = this.q2;
this.q2 = temp;
return res;
}
public int top() {
int size = q1.size();
for (int i = 0; i < size - 1; i++) {
this.q2.offer(q1.poll());
}
int res = this.q1.poll();
this.q2.offer(res);
Queue<Integer> temp = this.q1;
this.q1 = this.q2;
this.q2 = temp;
return res;
}
public boolean empty() {
return this.q1.isEmpty();
}
}