查看: 1760| 回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] 求助leet code 716 Max Stack

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
Design a max stack that supports push, pop, top, peekMax and popMax.

push(x) -- Push element x onto stack.
pop() -- Remove the element on top of the stack and return it.
top() -- Get the element on the top.
peekMax() -- Retrieve the maximum element in the stack.
popMax() -- Retrieve the maximum element in the stack, and remove it. If you find more than one maximum elements, only remove the top-most one.

Example 1:
MaxStack stack = new MaxStack();
stack.push(5);  
stack.push(1);
stack.push(5);
stack.top(); -> 5
stack.popMax(); -> 5
stack.top(); -> 1 // this step should be 1 or 5? 不是很明白为什么是1. 前几步有两个5被push进去了, 只pop Max了一个 5 难道不是应该还留下一个 5吗
stack.peekMax(); -> 5
stack.pop(); -> 1
stack.top(); -> 5

上一篇:为啥大家经常说Leetcode 400题?
下一篇:面试时所有的语言里没有现成的数据结构怎么办?
全局:
楼主你标注的那一行,应该top就是1吧,之前popmax了一个最顶上的5,不难理解吧?
回复

使用道具 举报

🔗
337845818 2019-4-28 07:13:21 | 只看该作者
全局:
top() -- Get the element on the top.
[5]
[5, 1]
[5, 1, 5]
pop max 显然是最后一个5 -> [5, 1]
top就是1呗.

补充内容 (2019-4-28 07:13):
popMax() -- Retrieve the maximum element in the stack, and remove it. If you find more than one maximum elements, only remove the top-most one.
回复

使用道具 举报

🔗
wisdompeak2 2019-4-29 14:25:01 | 只看该作者
全局:
我写过一段简单的分析,不知道你是否能看明白。
https://github.com/wisdompeak/Le ... esign/716.Max-Stack
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表