【Python】通过栈实现括号匹配

括号匹配 示例:

字符串中有括号”()[]{}”,设计算法,判断该字符串是否有效
括号必须以正确的顺序配对,如:“()”、“()[]”是有效的,但“([)]”无效

Python代码如下:

def match_parentheses(s):
    # 把一个list当做栈使用
    ls = []
    parentheses = "()[]{}"
    for i in range(len(s)):
        si = s[i]
        # 如果不是括号则继续
        if parentheses.find(si) == -1:
            continue
        # 左括号入栈
        if si == '(' or si == '[' or si == '{':
            ls.append(si)
            continue
        if len(ls) == 0:
            return False
        # 出栈比较是否匹配
        p = ls.pop()
        if (p == '(' and si == ')') or (p == '[' and si == ']') or (p == '{' and si == '}'):
            continue
        else:
            return False

    if len(ls) > 0:
        return False
    return True


if __name__ == '__main__':
    s = "{abc}{de}(f)[(g)"
    result = match_parentheses(s)
    print(s, result)
    s = "0{abc}{de}(f)[(g)]9"
    result = match_parentheses(s)
    print(s, result)

输出:

{abc}{de}(f)[(g) False
0{abc}{de}(f)[(g)]9 True

猜你喜欢

转载自blog.csdn.net/weixin_42018258/article/details/80579081
今日推荐