• 【leetcode】1541. Minimum Insertions to Balance a Parentheses String


    题目如下:

    Given a parentheses string s containing only the characters '(' and ')'. A parentheses string is balanced if:

    • Any left parenthesis '(' must have a corresponding two consecutive right parenthesis '))'.
    • Left parenthesis '(' must go before the corresponding two consecutive right parenthesis '))'.

    In other words, we treat '(' as openning parenthesis and '))' as closing parenthesis.

    For example, "())""())(())))" and "(())())))" are balanced, ")()""()))" and "(()))" are not balanced.

    You can insert the characters '(' and ')' at any position of the string to balance it if needed.

    Return the minimum number of insertions needed to make s balanced.

    Example 1:

    Input: s = "(()))"
    Output: 1
    Explanation: The second '(' has two matching '))', but the first '(' has only ')' matching. We need to to add one more ')' at the end of the string to be "(())))" which is balanced.
    

    Example 2:

    Input: s = "())"
    Output: 0
    Explanation: The string is already balanced.
    

    Example 3:

    Input: s = "))())("
    Output: 3
    Explanation: Add '(' to match the first '))', Add '))' to match the last '('.
    

    Example 4:

    Input: s = "(((((("
    Output: 12
    Explanation: Add 12 ')' to balance the string.
    

    Example 5:

    Input: s = ")))))))"
    Output: 5
    Explanation: Add 4 '(' at the beginning of the string and one ')' at the end. The string becomes "(((())))))))".
    

    Constraints:

    • 1 <= s.length <= 10^5
    • s consists of '(' and ')' only.

    解题思路:这种括号问题在leetcode有过很多了。思路都是一样的,用右括号去匹配之前出现过的左括号。

    代码如下:

    class Solution(object):
        def minInsertions(self, s):
            """
            :type s: str
            :rtype: int
            """
            res = 0
            left_count = 0
            right_count = 0
            for i in s:
                if i == '(':
                    if left_count == 0 or right_count == 0 :
                        left_count += 1
                    elif right_count == 1:
                        res += 1
                        right_count -= 1
                    else:right_count -= 2
                else:
                    if left_count == 0:
                        left_count += 1
                        right_count += 1
                        res += 1
                    elif right_count == 0:
                        right_count += 1
                    elif right_count == 1:
                        left_count -= 1
                        right_count -= 1
            if left_count > 0 :
                res += (left_count*2 - right_count)
    
            return res
  • 相关阅读:
    TensorRT 开始
    Vim 常用操作
    Android AsyncTask 的实现及 cancel 方式
    让 Andriod TextView 中的文本链接可点击的方法—— Linkify 及其他
    Android通过百度地图API用Service和Alarm在后台定时获取地理位置信息
    【转】Mac OS X 快捷键合集
    iframe导致的IE6下https页面安全提示
    [转载]IE的版本识别
    [转]iframe异步加载
    IE的html条件注释
  • 原文地址:https://www.cnblogs.com/seyjs/p/13667795.html
Copyright © 2020-2023  润新知