zoukankan      html  css  js  c++  java
  • 【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
  • 相关阅读:
    Angular7 表单
    使用Angular2的Http发送AJAX请求
    Nginx配置SSL证书实现https访问「浏览器未认证」
    详解 Nginx如何配置Web服务器
    前后端分离不可缺少的神器 NGINX
    程序员的快速开发框架:Github上 10 大优秀的开源后台控制面板
    腾讯出品的一个超棒的 Android UI 库
    Vue 旅游网首页开发1-工具安装及码云使用
    Angular routing生成路由和路由的跳转
    Angular 父子组件传值
  • 原文地址:https://www.cnblogs.com/seyjs/p/13667795.html
Copyright © 2011-2022 走看看