?
?
?
class="java" name="code">public interface IMinMaxStack<T> { public T pop(); public void push(T t); public T getMin(); public T getMax(); public int getLength(); }
?
public class MinMaxStack implements IMinMaxStack<Integer> { private static int maxLength = 5; private static int [] data= new int[maxLength]; private static int [] mins= new int[maxLength]; private static int [] maxs= new int[maxLength]; private static int size = 0; private static int current = 0; private static int total = 0; private void growing(){ if (current >= maxLength / 4 * 3 ) { maxLength = (maxLength *3)/2 + 1; data = Arrays.copyOf(data, maxLength); mins = Arrays.copyOf(mins, maxLength); maxs = Arrays.copyOf(maxs, maxLength); } } @Override public Integer pop() { total --; if (current >= 0) { int temp = data[current-1] ; current --; return temp; } return null; } @Override public void push(Integer t) { total ++; growing(); data[current] = t; if(current == 0){ mins[current] = t; maxs[current] = t; }else{ if (mins[current-1] >= t) { mins[current] = t; }else{ mins[current] = mins[current-1]; } if (maxs[current-1] <= t) { maxs[current] = t; }else{ maxs[current] = maxs[current-1]; } } current ++; } @Override public Integer getMin() { int temp = mins[current]; return temp; } @Override public Integer getMax() { int temp = maxs[current]; return temp; } @Override public int getLength() { return total; } public static void main(String[] args) { MinMaxStack ms = new MinMaxStack(); ms.push(6); ms.push(2); ms.push(7); ms.push(1); ms.push(5); ms.push(3); System.out.println("current\tlength\tpop\tmin\tmax"); System.out.println(ms.current+"\t"+ms.getLength()+"\t"+ms.pop()+"\t"+ ms.getMin()+"\t"+ ms.getMax()); System.out.println(ms.current+"\t"+ms.getLength()+"\t"+ms.pop()+"\t"+ ms.getMin()+"\t"+ ms.getMax()); System.out.println(ms.current+"\t"+ms.getLength()+"\t"+ms.pop()+"\t"+ ms.getMin()+"\t"+ ms.getMax()); System.out.println(ms.current+"\t"+ms.getLength()+"\t"+ms.pop()+"\t"+ ms.getMin()+"\t"+ ms.getMax()); System.out.println(ms.current+"\t"+ms.getLength()+"\t"+ms.pop()+"\t"+ ms.getMin()+"\t"+ ms.getMax()); System.out.println(ms.current+"\t"+ms.getLength()+"\t"+ms.pop()+"\t"+ ms.getMin()+"\t"+ ms.getMax()); } }
?
?
?
?
?
?
?
?
?
?
?
?
?
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(支持支付宝和微信 以及扣扣群),没钱捧个人场,谢谢各位。
?
个人主页:http://knight-black-bob.iteye.com/
?
?
?谢谢您的赞助,我会做的更好!