【剑指Offer】用两个队列实现栈 + 用两个栈实现队列【Java 栈 队列】

① 剑指 Offer 09. 用两个栈实现队列

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof

用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead
,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )

(1)Stack(效率比较低):

参考:《剑指Offer第二版》

	class CQueue {
		
		Stack<Integer> st1 = new Stack<>();
		Stack<Integer> st2 = new Stack<>();

	    public CQueue() {

	    }
	    
	    public void appendTail(int value) {
	    	st1.push(value);
	    }
	    
	    public int deleteHead() {
	    	if(st2.size() <= 0) {
	    		while(st1.size() > 0 ) {
		    		int value = st1.peek();
		    		st1.pop();
		    		st2.push(value);
		    	}
	    	}
	    	
	    	if(st2.size() <= 0) return -1;
	    	
	    	return st2.pop();
	    }
	}

	/**
	 * Your CQueue object will be instantiated and called as such:
	 * CQueue obj = new CQueue();
	 * obj.appendTail(value);
	 * int param_2 = obj.deleteHead();
	 */

(2)LinkedList(效率更高):

	class CQueue {
		LinkedList<Integer> st1 ;
		LinkedList<Integer> st2 ;
		
	    public CQueue() {
	    	st1 = new LinkedList<>();
	    	st2 = new LinkedList<>();
	    }
	    
	    public void appendTail(int value) {
	    	st1.offer(value);
	    }
	    
	    public int deleteHead() {
	    	if( st2.isEmpty() ) {
	    		while(!st1.isEmpty()) {
	    			int value = st1.pop();
	    			st2.offer(value);
	    		}
	    	}
	    	
	    	if(st2.isEmpty()) return -1;
	    	return st2.pop();
	    	
	    }
	}

/**
 * Your CQueue object will be instantiated and called as such:
 * CQueue obj = new CQueue();
 * obj.appendTail(value);
 * int param_2 = obj.deleteHead();
 */

② 225. 用队列实现栈

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-stack-using-queues

请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。

实现 MyStack 类:

void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。

注意:

你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty
这些操作。 你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 ,
只要是标准的队列操作即可。

	class MyStack {
		
		Queue<Integer> q1; //保持和栈一样的位置  比如插入 abc  , 队列排序为  a , b , c
		Queue<Integer> q2;  //辅助队列

	    public MyStack() {
	    	q1 = new LinkedList<>();
	    	q2 = new LinkedList<>();
	    }
	    
	    public void push(int x) {
	    	//保证新添加的元素在首位 (和栈的顺序相同)
	    	q2.offer(x);
	    	while( ! q1.isEmpty() ) {
	    		int value = q1.poll();
	    		q2.offer(value);
	    	}
	    	//交换q1 q2 , 保证q1 和 栈顺序相同, q2为空
	    	Queue<Integer> tempQ;
	    	tempQ  = q1;
	    	q1 = q2;
	    	q2 = tempQ;
	    }
	    
	    public int pop() {
	    	return q1.poll();
	    }
	    
	    public int top() {
	    	return q1.peek();
	    }
	    
	    public boolean empty() {
	    	return q1.isEmpty();
	    }
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值