Python 用栈判断括号匹配
#!/usr/bin/python# -*- coding: UTF-8 -*-
from pythonds.basic.stack import Stack
def parChecker(symbolString):s = Stack()
balanced = True
index = 0
while index < len(symbolString) and balanced:
symbol =symbolString[index]
#左边括号入栈if symbol == '(':
s.push(symbol)
else:
#如果栈提前为空,则表示前面匹配成功,后面没有匹配成功if s.isEmpty():
balanced = False
#右边括号出栈else:
s.pop()
index += 1
if balanced and s.isEmpty():return True
else:
return False
print(parChecker('()())'))
print(parChecker('()()()'))
print(parChecker('((()())())'))
以上是 Python 用栈判断括号匹配 的全部内容, 来源链接: utcz.com/z/389468.html