zoukankan      html  css  js  c++  java
  • 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('((()())())'))
  • 相关阅读:
    搜索区间
    搜索插入位置
    旋转排序数组
    搜索二维矩阵
    njnja 安装
    rpmbuild打包
    snappy 安装
    mysql8 安装
    re2c安装
    make 安装
  • 原文地址:https://www.cnblogs.com/boluo007/p/10117146.html
Copyright © 2011-2022 走看看