(symbolString)
| 51 | |
| 52 | # 利用栈判断括号平衡Balanced parentheses |
| 53 | def parChecker(symbolString): |
| 54 | s = Stack() |
| 55 | balanced = True |
| 56 | index = 0 |
| 57 | while index < len(symbolString) and balanced: |
| 58 | symbol = symbolString[index] |
| 59 | if symbol in '([{': |
| 60 | s.push(symbol) |
| 61 | else: |
| 62 | if s.isEmpty(): |
| 63 | balanced = False |
| 64 | else: |
| 65 | top = s.pop() |
| 66 | if not matches(top, symbol): |
| 67 | balanced = False |
| 68 | index += 1 |
| 69 | |
| 70 | if balanced and s.isEmpty(): |
| 71 | return True |
| 72 | else: |
| 73 | return False |
| 74 | |
| 75 | def matches(open, close): |
| 76 | opens = '([{' |