package com.company;
import java.util.Stack;
public class MyQueue {
private Stack<Integer> inputStack;
private Stack<Integer> outputStack;
public MyQueue(){
inputStack = new Stack<>();
outputStack = new Stack<>();
}
public void push(int x){
inputStack.push(x);
}
public int pop(){
if (outputStack.isEmpty()) {
while (!inputStack.isEmpty()) {
outputStack.push(inputStack.pop());
}
}
return outputStack.pop();
}
public int peek(){
if (outputStack.isEmpty()) {
while (!inputStack.isEmpty()) {
outputStack.push(inputStack.pop());
}
}
return outputStack.peek();
}
public boolean isEmpty(){
return inputStack.isEmpty()&&outputStack.isEmpty();
}
}
leetcode 232 用栈实现队列
最新推荐文章于 2024-08-06 12:37:30 发布