package question;import java.util.EmptyStackException;import java.util.Stack;/*@author: Roderland@create: 2020年08月18日---20:13*/public class Question232 {public static void main(String[] args) {MyQueue queue = new MyQueue();queue.push(1);queue.push(2);queue.peek(); // 返回 1queue.pop(); // 返回 1queue.empty(); // 返回 false}}class MyQueue {Stack<Integer> pushStack = new Stack<>();Stack<Integer> popStack = new Stack<>();/** Initialize your data structure here. */public MyQueue() {}/** Push element x to the back of queue. */public void push(int x) {pushStack.push(x);}/** Removes the element from in front of queue and returns that element. */public int pop() {if (empty()) throw new EmptyStackException();if (popStack.isEmpty()) {while (!pushStack.isEmpty()) {popStack.push(pushStack.pop());}}return popStack.pop();}/** Get the front element. */public int peek() {if (empty()) throw new EmptyStackException();if (popStack.isEmpty()) {while (!pushStack.isEmpty()) {popStack.push(pushStack.pop());}}return popStack.peek();}/** Returns whether the queue is empty. */public boolean empty() {return pushStack.isEmpty()&&popStack.isEmpty();}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。