题目链接: leetocde 1172
这个题目用 python 做很简单
python 中使用 list 实现栈
我们定义几个类的属性:
self.s = [[]]
,一个list 的 list ,这里面是我们要操作的所有的栈self.cur_id = 0
代表下一个 push 操作对应的栈的 idself.capacity
就是每个栈的容量操作:
class DinnerPlates: def __init__(self, capacity: int): self.capacity = capacity self.s = [[]] self.cur_id = 0 def push(self, val: int) -> None: while True: if len(self.s[self.cur_id]) < self.capacity: break self.cur_id += 1 if len(self.s) == self.cur_id: self.s.append([]) self.s[self.cur_id].append(val) def pop(self) -> int: i = len(self.s)-1 while not self.s[i]: if i == 0: return -1 self.s.pop() i -= 1 self.cur_id = min(self.cur_id, i) return self.s[i].pop() def popAtStack(self, index: int) -> int: if index < len(self.s) and self.s[index]: self.cur_id = min(self.cur_id, index) return self.s[index].pop() return -1 # Your DinnerPlates object will be instantiated and called as such: # obj = DinnerPlates(capacity) # obj.push(val) # param_2 = obj.pop() # param_3 = obj.popAtStack(index)