leetcode--括号有效性检查

给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
示例 1:
输入: "()"
输出: true
示例 2:
输入: "(]"
输出: false

# -*- coding:utf-8 -*-


def valid_bracket(str_list):
    bracket = {')': '(',
               '[': ']',
               '{': '}',
               }
    right_bracket = list(bracket.values())
    queue = []
    flag = False
    idx, length = 0, len(str_list)
    while idx < length:
        if str_list[idx] in bracket:
            queue.append(str_list[idx])
            flag = True
        elif str_list[idx] in right_bracket:
            if len(queue) <= 0:
                return False
            value = queue.pop()
            if value != str_list[idx]:
                return False

    if flag and len(queue) <= 0:
        return True

 

上一篇:Bracket Sequence CodeForces - 223A


下一篇:A. Anton and Letters//codeforces38