class MyQueue{
MyStack myStack1=new MyStack();
MyStack myStack2=new MyStack();
public void offer(int value){
myStack1.push(value);
}
public int poll(){
if(myStack2.isEmpty()){
while(!myStack1.isEmpty()){
myStack2.push(myStack1.pop());
}
return myStack2.pop();
}else{
return myStack2.pop();
}
}
public int getMax(){
return Math.max(myStack1.getMax(), myStack2.getMax());
}
}
class MyStack {
Stack<Integer> stack1=new Stack<Integer> ();
Stack<Integer> stack2=new Stack<Integer> ();
public boolean isEmpty(){
return stack1.isEmpty();
}
public void push(int value){
stack1.push(value);
if(stack2.isEmpty()){
stack2.push(value);
}else{
stack2.push(Math.max(value, stack2.peek()));
}
}
public int pop(){
stack2.pop();
return stack1.pop();
}
public int getMax(){
if(stack2.isEmpty()){
return 0;
}
return stack2.peek();
}
}
Java实现-队列中取最大值操作
最新推荐文章于 2024-02-19 17:38:05 发布