使用辅助来存储每次入栈后的最小值
package test;
import java.util.Arrays;
public class Test2 {
static int max = 10;
static int n = -1;
static int[]
stack = new int[max],
hstack = new int[max];
static void pop(){
stack[n] = -100000;
hstack[n] = 1000000;
n--;
}
static void push(int num){
stack[n+1] = num;
if(n==-1 || hstack[n]>num){
hstack[n+1] = num;
}else{
hstack[n+1] = hstack[n];
}
n++;
}
static int min(){
return hstack[n];
}
public static void main(String[] args) {
Arrays.fill(stack, -100000);
Arrays.fill(hstack, 100000);
push(21);System.out.println(min());
push(23);System.out.println(min());
push(19);System.out.println(min());
push(18);System.out.println(min());
pop();System.out.println(min());
}
}