先进后出之最优栈
程序员文章站
2024-03-17 22:00:28
...
进出栈时标记索引
package Demo;
public class Stack {
private int [] data = new int[20];
private int index = -1;
public void push(int newint) {
if(index+1 == data.length) {
int [] datanew = new int[data.length * 2];
for(int i = 0; i < data.length; i++) {
datanew[i] = data[i];
}
data = datanew;
}
data[index + 1] = newint;
index++;
}
public Integer pop() {
if(index == -1) {
return null;
}
int value = data[index];
index--;
if(index < data.length/4) {
int size = data.length/2;
size = size < 20 ? 20 :size;
int[] datanew = new int[size];
for(int i = 0; i < size; i++) {
datanew[i] = data[i];
}
data = datanew;
}
return value;
}
}
上一篇: 最长公共子序列