FreeCMS商业版

功能强大、开源、代码通俗易懂、注释详细、面向二次开发友好!

承接网站建设、软件开发、微信开发项目,价格从优,欢迎咨询合作!

FreeCMS商业版V2.8已发布!购买请联系客服QQ 124812878 查看更新日志



在线演示 立即购买 下载免费版 下载商业版用户手册
客服QQ: 124812878 联系电话: 18339991503 Email: freeteam@foxmail.com

最小栈 三种实现(面试...) - Ctrl+C&Ctrl+V - ITeye博客

加入收藏夹】     【打印】     【关闭】 来源: 日期:2018-04-03 22:00:06 点击量: 收藏

 

 

问题:实现一个栈,带有出栈(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<Student> ms = new MinStack();
		ms.push(s);
		ms.push(s2);
		
		Student min = ms.getMin();
		System.out.println(min);
	}

}
class MinStack<T extends Comparable<T>>{
	Stack<T> stack;
	Stack<T> minStack;
	
    public void push(T t){
    	T obj  = null;
    	if (stack == null) {
			stack = new Stack<T>();
			minStack = new Stack<T>();
			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<Student>{

	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<Student> msun = new MinStackUseNode<Student>();
		msun.push(s);
		msun.push(s2);

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

}

class MinStackUseNode<T extends Comparable<T>> {
	public MinStackElem<T> top;

	public <T extends Comparable<T>> 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<T extends Comparable<T>> {
	public T data;
	public T min;
	public MinStackElem next;

	http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/ public MinStackNode(){}
	public MinStackElem(T data, T min, MinStackElem next) {
		this.data = data;
		this.min = min;
		this.next = next;
	}

}

 

3.使用数组

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 http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/ 4  * 3 ) {
			maxLength = (maxLength *3)http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/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:http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/



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


上一条


下一条

相关新闻
自定义表单

本例固定链接: 分享到:
更多