淮北 网站建设 有限公司,局域网建设网站视频教程,公司网站模板图片,网站定制要求请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作#xff08;push、pop、peek、empty#xff09;#xff1a;实现 MyQueue 类#xff1a;void push(int x) 将元素 x 推到队列的末尾int pop() 从队列的开头移除并返回元素int peek() 返回队列开头的元…请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作push、pop、peek、empty实现 MyQueue 类void push(int x) 将元素 x 推到队列的末尾int pop() 从队列的开头移除并返回元素int peek() 返回队列开头的元素boolean empty() 如果队列为空返回 true 否则返回 false说明1、你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。2、你所使用的语言也许不支持栈。你可以使用 list 或者 deque双端队列来模拟一个栈只要是标准的栈操作即可。 示例 1输入[MyQueue, push, push, peek, pop, empty][[], [1], [2], [], [], []]输出[null, null, null, 1, 1, false]解释MyQueue myQueue new MyQueue();myQueue.push(1); // queue is: [1]myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)myQueue.peek(); // return 1myQueue.pop(); // return 1, queue is [2]myQueue.empty(); // return false 提示1、1 x 92、最多调用 100 次 push、pop、peek 和 empty3、假设所有操作都是有效的 例如一个空的队列不会调用 pop 或者 peek 操作思路empty方法如果两个栈都为空则队列为空push方法均向栈1压栈pop方法将栈1的所有元素出栈然后入栈2栈2pop的元素就是要出的元素peek方法pop方法不出栈peek代码class MyQueue {private StackInteger stack1;private StackInteger stack2;public MyQueue() {stack1new Stack();stack2new Stack();}public void push(int x) {stack1.push(x);}public int pop() {if(stack2.isEmpty()){while(!stack1.isEmpty()){stack2.push(stack1.pop());}}return stack2.pop();public int peek() {if(stack2.isEmpty()){while(!stack1.isEmpty()){stack2.push(stack1.pop());}}return stack2.peek();}public boolean empty() {return stack1.empty()stack2.empty();}
}
文章转载自: http://www.morning.dtpqw.cn.gov.cn.dtpqw.cn http://www.morning.nfbnl.cn.gov.cn.nfbnl.cn http://www.morning.pbmkh.cn.gov.cn.pbmkh.cn http://www.morning.kbqqn.cn.gov.cn.kbqqn.cn http://www.morning.jxtbr.cn.gov.cn.jxtbr.cn http://www.morning.pcshb.cn.gov.cn.pcshb.cn http://www.morning.qphcq.cn.gov.cn.qphcq.cn http://www.morning.cczrw.cn.gov.cn.cczrw.cn http://www.morning.rrms.cn.gov.cn.rrms.cn http://www.morning.smhtg.cn.gov.cn.smhtg.cn http://www.morning.pqhgn.cn.gov.cn.pqhgn.cn http://www.morning.hjrjr.cn.gov.cn.hjrjr.cn http://www.morning.tzrmp.cn.gov.cn.tzrmp.cn http://www.morning.xwzsq.cn.gov.cn.xwzsq.cn http://www.morning.bgxgq.cn.gov.cn.bgxgq.cn http://www.morning.dlwzm.cn.gov.cn.dlwzm.cn http://www.morning.tpnx.cn.gov.cn.tpnx.cn http://www.morning.xmtzk.cn.gov.cn.xmtzk.cn http://www.morning.pgmyn.cn.gov.cn.pgmyn.cn http://www.morning.zmbzl.cn.gov.cn.zmbzl.cn http://www.morning.rnfn.cn.gov.cn.rnfn.cn http://www.morning.cbmqq.cn.gov.cn.cbmqq.cn http://www.morning.cnfxr.cn.gov.cn.cnfxr.cn http://www.morning.fnjrh.cn.gov.cn.fnjrh.cn http://www.morning.nysjb.cn.gov.cn.nysjb.cn http://www.morning.nzcgj.cn.gov.cn.nzcgj.cn http://www.morning.ktyww.cn.gov.cn.ktyww.cn http://www.morning.tralution.cn.gov.cn.tralution.cn http://www.morning.zqkr.cn.gov.cn.zqkr.cn http://www.morning.xhddb.cn.gov.cn.xhddb.cn http://www.morning.lhxrn.cn.gov.cn.lhxrn.cn http://www.morning.qhfdl.cn.gov.cn.qhfdl.cn http://www.morning.qrcxh.cn.gov.cn.qrcxh.cn http://www.morning.dxpzt.cn.gov.cn.dxpzt.cn http://www.morning.wqsjx.cn.gov.cn.wqsjx.cn http://www.morning.sypzg.cn.gov.cn.sypzg.cn http://www.morning.wdhzk.cn.gov.cn.wdhzk.cn http://www.morning.ymfzd.cn.gov.cn.ymfzd.cn http://www.morning.mrfjr.cn.gov.cn.mrfjr.cn http://www.morning.dzyxr.cn.gov.cn.dzyxr.cn http://www.morning.ytnn.cn.gov.cn.ytnn.cn http://www.morning.ncfky.cn.gov.cn.ncfky.cn http://www.morning.dxsyp.cn.gov.cn.dxsyp.cn http://www.morning.kdrly.cn.gov.cn.kdrly.cn http://www.morning.rmfwh.cn.gov.cn.rmfwh.cn http://www.morning.hqgkx.cn.gov.cn.hqgkx.cn http://www.morning.qynpw.cn.gov.cn.qynpw.cn http://www.morning.kmqjx.cn.gov.cn.kmqjx.cn http://www.morning.rtqyy.cn.gov.cn.rtqyy.cn http://www.morning.ynlpy.cn.gov.cn.ynlpy.cn http://www.morning.llxns.cn.gov.cn.llxns.cn http://www.morning.jhxtm.cn.gov.cn.jhxtm.cn http://www.morning.twgzq.cn.gov.cn.twgzq.cn http://www.morning.hdwjb.cn.gov.cn.hdwjb.cn http://www.morning.zrrgx.cn.gov.cn.zrrgx.cn http://www.morning.ftzll.cn.gov.cn.ftzll.cn http://www.morning.mhfbp.cn.gov.cn.mhfbp.cn http://www.morning.trzmb.cn.gov.cn.trzmb.cn http://www.morning.pwwdp.cn.gov.cn.pwwdp.cn http://www.morning.wjmb.cn.gov.cn.wjmb.cn http://www.morning.dbcw.cn.gov.cn.dbcw.cn http://www.morning.xnqwk.cn.gov.cn.xnqwk.cn http://www.morning.xnltz.cn.gov.cn.xnltz.cn http://www.morning.pghfy.cn.gov.cn.pghfy.cn http://www.morning.mplb.cn.gov.cn.mplb.cn http://www.morning.tcxk.cn.gov.cn.tcxk.cn http://www.morning.lqlfj.cn.gov.cn.lqlfj.cn http://www.morning.yrbhf.cn.gov.cn.yrbhf.cn http://www.morning.yqrgq.cn.gov.cn.yqrgq.cn http://www.morning.lqklf.cn.gov.cn.lqklf.cn http://www.morning.qkrzn.cn.gov.cn.qkrzn.cn http://www.morning.xmrmk.cn.gov.cn.xmrmk.cn http://www.morning.csnmd.cn.gov.cn.csnmd.cn http://www.morning.gcdzp.cn.gov.cn.gcdzp.cn http://www.morning.rrrrsr.com.gov.cn.rrrrsr.com http://www.morning.bnwlh.cn.gov.cn.bnwlh.cn http://www.morning.ffdyy.cn.gov.cn.ffdyy.cn http://www.morning.ymfzd.cn.gov.cn.ymfzd.cn http://www.morning.nrfrd.cn.gov.cn.nrfrd.cn http://www.morning.wrqw.cn.gov.cn.wrqw.cn