#!/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('((()())())'))

 

相关文章:

  • 2021-07-06
  • 2021-11-05
  • 2021-11-21
  • 2022-12-23
  • 2021-06-09
  • 2022-12-23
  • 2022-12-23
  • 2021-05-18
猜你喜欢
  • 2021-10-11
  • 2021-08-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-21
  • 2022-12-23
相关资源
相似解决方案