力扣 20. 有效的括号

栈 + hash表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def isValid(s):
if not s: return True
pip = {"}": "{",
")": "(",
"]": "["}

queue = []

for sign in s:
if sign in pip.keys():
if not queue:
return False
else:
cur = queue.pop()
if pip[sign] != cur:
return False
else:
queue.append(sign)


if not queue:
return True
else:
return False
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信