# 代码随想录训练营第III期--011--python# 20. 有效的括号classSolution:defisValid(self, s:str)->bool:
d ={'(':')','[':']','{':'}'}
res =[]for i in s:if res !=[]and i == res[-1]:
res.pop()else:try:
res.append(d[i])except:returnFalsereturn res ==[]# 1047. 删除字符串中的所有相邻重复项defremoveDuplicates(self, s:str)->str:
res =[]for i in s:if res and res[-1]== i:
res.pop()else:
res.append(i)return''.join(res)# 150. 逆波兰表达式求值defevalRPN(self, tokens: List[str])->int:
res =[]for i in tokens:if i =='+':
res.append(res.pop()+ res.pop())elif i =='-':
res.append(-res.pop()+ res.pop())elif i =='*':
res.append(res.pop()* res.pop())elif i =='/':
res.append(int(1/res.pop()* res.pop()))else:
res.append(int(i))return res[-1]