【Leetcode】155. Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

push(x) -- Push element x onto stack.

pop() -- Removes the element on top of the stack.

top() -- Get the top element.

getMin() -- Retrieve the minimum element in the stack.

1 在init函数中,绑定类属性的时候要用self.

2 在init中,绑定一个类属性是一个stack q

3 写push函数的时候,除了要append数以外,还要注意要更新curMin

4 注意此题中push的是一个tuple,里面有x和min值


要写成curMin==None才行,我之前写成not curMin是代表curMin为0了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容