-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMyStack.java
66 lines (57 loc) · 1.37 KB
/
MyStack.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
package ds.queue.leetcode225;
import java.util.ArrayDeque;
import java.util.Deque;
/**
* 用队列实现栈
* LeetCode 225 https://leetcode-cn.com/problems/implement-stack-using-queues/
*
* @author yangyi 2021年07月09日18:32:47
*/
public class MyStack {
private Deque<Integer> deque;
/**
* Initialize your data structure here.
*/
public MyStack() {
deque = new ArrayDeque<>();
}
/**
* Push element x onto stack.
*/
public void push(int x) {
deque.offer(x);
}
/**
* Removes the element on top of the stack and returns that element.
*/
public int pop() {
for (int i = 0; i < deque.size() - 1; i++) {
int first = deque.pollFirst();
deque.offerLast(first);
}
return deque.pollFirst();
}
/**
* Get the top element.
*/
public int top() {
return deque.peekLast();
}
/**
* Returns whether the stack is empty.
*/
public boolean empty() {
return deque.isEmpty();
}
public static void main(String[] args) {
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
// 返回 2
System.out.println(myStack.top());
// 返回 2
System.out.println(myStack.pop());
// 返回 False
System.out.println(myStack.empty());
}
}