from pythonds.basic import Stack#导包
def parChecker(symbolString):#定义了一个函数,函数名为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 =index+1#改变位置
if balanced and s.isEmpty():
return True
else:
return False
if __name__ == '__main__':
test = parChecker("(()")
print(test)
利用栈思想实现括号匹配(使用py)
最新推荐文章于 2024-08-10 23:14:03 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)