最小栈 三种实现(面试...)

阅读更多

 

 

问题:实现一个栈,带有出栈(pop),入栈(push),取最小元素(getMin)三个方法。要保证这三个方法的时间复杂度都是O(1)。

 

 

1.使用 两个栈实现

public class MinStackWithStack {

	 public static void main(String[] args) {
		Student s = new Student();
		s.stuAge = 28;
		s.stuName ="baoyou";
		Student s2 = new Student();
		s2.stuAge = 1;
		s2.stuName ="baoyuqi";
		
		MinStack ms = new MinStack();
		ms.push(s);
		ms.push(s2);
		
		Student min = ms.getMin();
		System.out.println(min);
	}

}
class MinStack>{
	Stack stack;
	Stack minStack;
	
    public void push(T t){
    	T obj  = null;
    	if (stack == null) {
			stack = new Stack();
			minStack = new Stack();
			minStack.push(t); 
		}else{
			obj = minStack.peek();
			if(t.compareTo(obj) >= 0){
				minStack.push(obj);
			}else{
				minStack.push(t);
			} 
		} 
    	stack.push(t);
    }	
	
    public T pop(){
    	if (stack == null || stack.empty()) {
    		return null;
		} 
    	return stack.pop();
    } 
    
    public T getMin(){
    	if (minStack == null || minStack.empty()) {
    		return null;
		} 
    	return minStack.peek();
    }
    
}
class Student implements Comparable{

	public String stuName;
	public int stuAge;
	
	@Override
	public int compareTo(Student s) { 
		if (this.stuAge < s.stuAge ) {
			return -1;
		}else if(this.stuAge > s.stuAge ){
			return 1;
		}
		return 0;
	}
	
	@Override
	public String toString() { 
		return FastJsonUtils.toJSONString(this).toString();
	}
}

 

2.使用 链表实现 栈

public class MinStackUseNodeDemo {

	public static void main(String[] args) {
		Student s = new Student();
		s.stuAge = 28;
		s.stuName = "baoyou";
		Student s2 = new Student();
		s2.stuAge = 1;
		s2.stuName = "baoyuqi";
		Student s3 = new Student();
		s3.stuAge = 29;
		s3.stuName = "baopan";
		
		MinStackUseNode msun = new MinStackUseNode();
		msun.push(s);
		msun.push(s2);

		Student min = msun.getMin();
		System.out.println(min);
	}

}

class MinStackUseNode> {
	public MinStackElem top;

	public > void push(T obj) {
		if (top == null) {
			top = new MinStackElem(obj, obj, null);
		} else {
			T min = null;
			if (obj.compareTo((T) top.min) >= 0) {
				min = (T) top.min;
			} else {
				min = obj;
			}
			MinStackElem minStackElem = new MinStackElem(obj, min, top);
			top = minStackElem;
		}
	}

	public T pop() {
		if (top == null) {
			return null;
		}
		T t = top.data;
		top = top.next;
		return t;
	}

	public T getMin() {
		if (top == null) {
			return null;
		}
		T t = top.min;
		return t;
	}

}

class MinStackElem> {
	public T data;
	public T min;
	public MinStackElem next;

	// public MinStackNode(){}
	public MinStackElem(T data, T min, MinStackElem next) {
		this.data = data;
		this.min = min;
		this.next = next;
	}

}

 

3.使用数组

public interface IMinMaxStack {  
  
    public T pop();  
    public void push(T t);  
    public T getMin();  
    public T getMax();  
    public int getLength();  
}

 

public class MinMaxStack implements IMinMaxStack {

	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/


最小栈 三种实现(面试...)_第1张图片最小栈 三种实现(面试...)_第2张图片最小栈 三种实现(面试...)_第3张图片
 
 
 谢谢您的赞助,我会做的更好!

你可能感兴趣的:(最小栈,三种,实现,java,O(1))