-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinStack.java
More file actions
50 lines (42 loc) · 777 Bytes
/
MinStack.java
File metadata and controls
50 lines (42 loc) · 777 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
50
package leetode;
import java.util.ArrayList;
import java.util.List;
class MinStack
{
List<Integer> al;
int min;
public MinStack()
{
this.al=new ArrayList<Integer>();
this.min=Integer.MAX_VALUE;
}
public void push(int x)
{
this.al.add(x);
if(x<this.min)
{
min=x;
}
}
public void pop()
{
int pop=al.remove(al.size()-1);
if(pop==min)
{
this.min=Integer.MAX_VALUE;
for(int list:al)
{
if(list<this.min)
this.min=list;
}
}
}
public int top()
{
return al.get(al.size()-1);
}
public int getMin()
{
return this.min;
}
}