目录
- [20. 有效的括号](#20. 有效的括号)
- [1047. 删除字符串中的所有相邻重复项](#1047. 删除字符串中的所有相邻重复项)
- [150. 逆波兰表达式求值](#150. 逆波兰表达式求值)
20. 有效的括号
python
class Solution:
def isValid(self, s: str) -> bool:
closeToOpen = {")": "(", "]":"[", "}":"{"}
stack = []
for string in s:
if string not in closeToOpen:
stack.append(string)
else:
if not stack or stack.pop() != closeToOpen[string]:
return False
return len(stack) == 0
1047. 删除字符串中的所有相邻重复项
python
class Solution:
def removeDuplicates(self, s: str) -> str:
stack = []
for string in s:
if not stack:
stack.append(string)
else:
if string == stack[-1]:
stack.pop()
else:
stack.append(string)
return ''.join(stack)
150. 逆波兰表达式求值
python
from operator import add, mul, sub
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
stack = []
op_map = {"+": add, "-": sub, "*": mul, "/": lambda x,y: int(x/y)}
for token in tokens:
if token not in op_map:
stack.append(token)
else:
y = int(stack.pop())
x = int(stack.pop())
res = op_map[token](x, y)
stack.append(res)
return int(stack[-1])