week 1: 实现一个特殊的栈,在实现栈的基本功能的基础上,在实现返回栈中最小的元素的操作。

topic requirement:
1.pop,push,getmin,操作的时间复杂度都是0( 1 );
2.设计栈的类型可以使用现成的栈的结构。

 

thoughts:考虑时间复杂度是O(1),用两个栈来实现此特殊栈。

the first method:

             两个栈同时压入,同时弹出

the second method:

              当前数<=min栈,压入min栈  ;当前数=min栈栈顶,data栈和min栈同时弹出

the code first approach:

public class GetMinStack1 {
	public static class SelfStack1{
		private Stack<Integer> stackData;
		private Stack<Integer> stackMin;
		
		public SelfStack1() {
			this.stackData = new Stack<Integer>();
			this.stackMin = new Stack<Integer>();
		}
		
		public void push(int newNum) {
			if(this.stackMin.isEmpty()) {
				this.stackMin.push(newNum);
			}else if(newNum<this.getmin()) {
				this.stackMin.push(newNum);
			}
			this.stackData.push(newNum);
		}
		
		public int getmin() {
			if(this.stackMin.isEmpty())
				throw new RuntimeException("Your stack is empty");
			return this.stackMin.peek();
		}
		
		public int pop() {
			if(this.stackData.isEmpty())
				throw new RuntimeException("Your stack is empty");
			this.stackMin.pop();
			return this.stackData.pop();
		}
		
	}
	
	public static void main(String[] args) {

		
		SelfStack1 stack1 = new SelfStack1();
		stack1.push(23);
		System.out.println(stack1.getmin()); //23
		stack1.push(2);
		System.out.println(stack1.getmin());//2
		stack1.push(3);      
		System.out.println(stack1.getmin());  //2
		System.out.println(stack1.pop());    //3
		System.out.println(stack1.getmin());  //2
	}

}

the code second approach:

public class GetMinStack2 {
	public static class  SelfStack2{
		private Stack<Integer> stackData;
		private Stack<Integer> stackMin;
		
		public SelfStack2() {
			this.stackData = new Stack<Integer>();
			this.stackMin = new Stack<Integer>();
		}
		
		public void push(int newNum) {
			if(this.stackMin.isEmpty()) {
				this.stackMin.push(newNum);
			} else if(newNum <= this.getmin()) {
				this.stackMin.push(newNum);
			}
			this.stackData.push(newNum);
			
		}
	    
	    public int getmin() {
	    	if(this.stackMin.isEmpty()) {
	    		throw new RuntimeException("Your stack is empty");
	    	}
	    	return this.stackMin.peek();	
     	}
	    
	    public int pop() {
	    	if(this.stackData.isEmpty())
	    		throw new RuntimeException("Your stack is empty");
	    	int value = this.stackData.pop();
	    	if(value == this.getmin())
	    		this.stackMin.pop();
	    	return value;
	    }
}
	
	public static void main(String[] args) {

		
		SelfStack2 stack1 = new SelfStack2();
		stack1.push(23);
		System.out.println(stack1.getmin()); //23
		stack1.push(2);
		System.out.println(stack1.getmin());//2
		stack1.push(3);      
		System.out.println(stack1.getmin());  //2
		System.out.println(stack1.pop());    //3
		System.out.println(stack1.getmin());  //2
	}

}

 reference:左神   

encouragement:  

             不积跬步,无以至千里

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

const

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值