max stack

import java.util.LinkedList;class MaxStack { Listlist; Queuequeue;

/** initialize your data structure here. */

public MaxStack() {

list = new ArrayList<>();

queue = new PriorityQueue<>(10000, Collections.reverseOrder());

}

public void push(int x) {

list.add(x);

queue.offer(x);

}

public int pop() {

int temp = list.remove(list.size() - 1);

queue.remove(temp);

return temp;

}

public int top() {

return list.get(list.size() - 1);

}

public int peekMax() {

return queue.peek();

}

public int popMax() {

Integer temp = queue.poll();

for(int i = list.size() - 1; i >= 0; i--){

if(list.get(i).equals(temp)){

list.remove(i);

break;

}

}

return (int)temp;

}

}

/**

* Your MaxStack object will be instantiated and called as such:

* MaxStack obj = new MaxStack();

* obj.push(x);

* int param_2 = obj.pop();

* int param_3 = obj.top();

* int param_4 = obj.peekMax();

* int param_5 = obj.popMax();

*/

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,356评论 0 33
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,850评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 32,512评论 18 399
  • 一、 1、请用Java写一个冒泡排序方法 【参考答案】 public static void Bubble(int...
    独云阅读 5,255评论 0 6
  • 我是一个大三的学生,一个月前找了一个兼职。成为了一名销售助理。 问我兼职的目的吗?很简单。1,锻炼自己,想让自己忙...
    我是平民我叫阿男阅读 2,910评论 0 0

友情链接更多精彩内容