#!/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(‘((()())())‘))
原文地址:https://www.cnblogs.com/boluo007/p/10117146.html
时间: 2024-09-30 10:29:53