java 最小栈_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java 最小栈

java 最小栈

 2017/11/1 0:11:39  knight_black_bob  程序员俱乐部  我要评论(0)
  • 摘要:publicinterfaceIMinMaxStack<T>{publicTpop();publicvoidpush(Tt);publicTgetMin();publicTgetMax();publicintgetLength();}publicclassMinMaxStackimplementsIMinMaxStack<Integer>{privatestaticintmaxLength=5
  • 标签:Java

?

?

?

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/



?
?
?谢谢您的赞助,我会做的更好!

上一篇: Spi扩展加载机制 下一篇: 没有下一篇了!
发表评论
用户名: 匿名