题目1522:包含min函数的栈
两个栈s1,s2(保存最小元素),压栈时,先把元素压入s1,如果该元素比s2栈顶元素还要小,则也把该元素压入s2。
弹栈时,先把元素弹出s1,如果该元素等于s2栈顶元素,则也把该元素弹出s2。.
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Stack;
public class S21 {
static Stack<Integer> s1;
static Stack<Integer> s2;
public static void main(String[] args) throws FileNotFoundException {
BufferedInputStream in = new BufferedInputStream(new FileInputStream("in.in"));
System.setIn(in);
Scanner cin = new Scanner(System.in);
while (cin.hasNextLine()) {
int n = Integer.parseInt(cin.nextLine());
s1 = new Stack<Integer>();
s2 = new Stack<Integer>();
for(int i=0; i<n; i++){
String s = cin.nextLine();
String[] ss = s.split(" ");
if(ss[0].equals("s")){
int val = Integer.parseInt(ss[1]);
s1.push(val);
if(s2.isEmpty() || val<s2.peek()){
s2.push(val);
}
}else if(ss[0].equals("o")){
int val = s1.pop();
if(val == s2.peek()){
s2.pop();
}
}
if(s2.isEmpty()){
System.out.println("NULL");
}else{
System.out.println(s2.peek());
}
}
}
}
public static void push(){
}
}