最小栈 三种实现(面试...)_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 最小栈 三种实现(面试...)

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

 2018/4/2 15:11:44  knight_black_bob  程序员俱乐部  我要评论(0)
  • 摘要:问题:实现一个栈,带有出栈(pop),入栈(push),取最小元素(getMin)三个方法。要保证这三个方法的时间复杂度都是O(1)。1.使用两个栈实现publicclassMinStackWithStack{publicstaticvoidmain(String[]args){Students=newStudent();s.stuAge=28;s.stuName="baoyou";Students2=newStudent();s2.stuAge=1;s2.stuName="baoyuqi"
  • 标签:面试 实现

?

?

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

?

?

1.使用 两个栈实现

class="java">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;

	// 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 / 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/



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

发表评论
用户名: 匿名