目录
前言
一、栈:有效的括号,最小栈,字符串解码,每日温度,柱状图中最大的矩形。
二、堆:数组中的第K个最大元素,前K个高频元素,数据流中的中位数。
一、栈
1. 有效的括号

class Solution(object):
def isValid(self, s):
dicts = {
')':'(',
']': '[',
'}': '{'
}
stack = []
for s_ in s:
if stack and s_ in dicts:
if stack[-1] == dicts[s_]:
stack.pop()
else:
return False
else:
stack.append(s_)
return not stack
2. 最小栈

class MinStack(object):
def __init__(self):
self.lst = []
def push(self, val):
self.lst.append(val)
def pop(self):
self.lst.pop()
def top(self):
return self.lst[-1]
def getMin(self):
return min(self.lst)
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
#

4188

被折叠的 条评论
为什么被折叠?



