-
Notifications
You must be signed in to change notification settings - Fork 7
/
demo1.txt
71 lines (60 loc) · 1.24 KB
/
demo1.txt
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
package chapter1;
import java.util.Iterator;
import java.util.Stack;
//我用了迭代器的方法,不过确实复杂度是书上的低一点,只不过空间复杂度这里低点
public class demo1 {
public static Stack<Integer> stack1;
public static Stack<Integer> stack2;
public static void main(String[] args) {
stack1 = new Stack<Integer>();
stack2 = new Stack<Integer>();
demo1 my = new demo1();
my.push(2);
my.push(6);
my.push(1);
my.push(9);
System.out.println(my.getMin());
}
//插入数据push
public void push(int a){
stack1.push(a);
if(stack2.isEmpty()){
stack2.push(a);
}
else{
if(stack2.peek()>a){
stack2.push(a);
}
}
}
//pop
public int pop(){
int value = stack1.pop();
if(!stack2.isEmpty() && value==stack2.peek()){
stack2.pop();
}
return value;
}
public int getMin(){
int value1 = stack2.pop();
if(!stack1.isEmpty()&&stack1.peek()==value1){
stack1.pop();
}
return value1;
}
/*
//获取最小值
public static int getMin(){
//可以对栈进行迭代遍历吧
Iterator<Integer> it = stack1.iterator();
int min = stack1.peek();
while(it.hasNext()){
int temp = it.next();
if(min>temp){
min = temp;
}
}
return min;
}
*/
}