1111. 有效括号的嵌套深度







class Solution(object):
    def maxDepthAfterSplit(self, seq):
        """
        :type seq: str
        :rtype: List[int]
        """
        res = []
        if not seq:
            return res
        depth, max_depth = 0, 0
        for ch in seq:
            if ch == "(":
                depth += 1
                if depth > max_depth:
                    max_depth = depth
            else:
                depth -= 1
        a_depth = 0
        mid = 1 + (max_depth-1)//2
        for ch in seq:
            if ch == "(":
                if a_depth < mid:
                    res.append(0)
                    a_depth += 1
                else:
                    res.append(1)
            else:
                if a_depth > 0:
                    res.append(0)
                    a_depth -= 1
                else:
                    res.append(1)
        return res

if __name__ == '__main__':
    solution = Solution()
    print(solution.maxDepthAfterSplit(seq="(()())"))
原文地址:https://www.cnblogs.com/panweiwei/p/13065301.html