1 Star 0 Fork 0

yuhang2__2/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
design-a-stack-with-increment-operation.py 921 Bytes
一键复制 编辑 原始数据 按行查看 历史
# Time: cotr: O(1)
# push: O(1)
# pop: O(1)
# increment: O(1)
# Space: O(n)
class CustomStack(object):
def __init__(self, maxSize):
"""
:type maxSize: int
"""
self.__max_size = maxSize
self.__stk = []
def push(self, x):
"""
:type x: int
:rtype: None
"""
if len(self.__stk) == self.__max_size:
return
self.__stk.append([x, 0])
def pop(self):
"""
:rtype: int
"""
if not self.__stk:
return -1
x, inc = self.__stk.pop()
if self.__stk:
self.__stk[-1][1] += inc
return x + inc
def increment(self, k, val):
"""
:type k: int
:type val: int
:rtype: None
"""
i = min(len(self.__stk), k)-1
if i >= 0:
self.__stk[i][1] += val
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yuhang2__2/LeetCode-Solutions.git
git@gitee.com:yuhang2__2/LeetCode-Solutions.git
yuhang2__2
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助