forked from lizeyang18/byteDanceAlgorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest30.java
More file actions
49 lines (42 loc) · 1019 Bytes
/
test30.java
File metadata and controls
49 lines (42 loc) · 1019 Bytes
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
package byteDance;
import java.util.Stack;
/**
* Created by lizeyang on 2020/5/15.
* 最大栈
*/
public class test30 {
static Stack<Integer> stack1 = new Stack<>();
static Stack<Integer> stack2 = new Stack<>();
public static void push(int num) {
if (stack1.isEmpty()) {
stack1.push(num);
stack2.push(num);
} else {
int tmp = stack2.peek();
if (num > tmp) {
stack2.push(num);
} else {
stack2.push(tmp);
}
stack1.push(num);
}
}
public static int pop() {
int res = stack1.pop();
stack2.pop();
return res;
}
public static int getMax() {
int res = 0;
if (!stack2.isEmpty()) {
res = stack2.peek();
}
return res;
}
public static void main(String[] args) {
push(1);
push(2);
System.out.println(getMax());
System.out.println(pop());
}
}