{"id":547,"date":"2020-09-26T10:54:26","date_gmt":"2020-09-26T02:54:26","guid":{"rendered":"http:\/\/39.96.58.60\/?p=547"},"modified":"2022-10-18T16:38:54","modified_gmt":"2022-10-18T08:38:54","slug":"leetcode-921-%e4%bd%bf%e6%8b%ac%e5%8f%b7%e6%9c%89%e6%95%88%e7%9a%84%e6%9c%80%e5%b0%91%e6%b7%bb%e5%8a%a0","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=547","title":{"rendered":"Leetcode 921. \u4f7f\u62ec\u53f7\u6709\u6548\u7684\u6700\u5c11\u6dfb\u52a0"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u7531&nbsp;<code>'('<\/code>&nbsp;\u548c&nbsp;<code>')'<\/code>&nbsp;\u62ec\u53f7\u7ec4\u6210\u7684\u5b57\u7b26\u4e32 <code>S<\/code>\uff0c\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u6700\u5c11\u7684\u62ec\u53f7\uff08 <code>'('<\/code>&nbsp;\u6216\u662f&nbsp;<code>')'<\/code>\uff0c\u53ef\u4ee5\u5728\u4efb\u4f55\u4f4d\u7f6e\uff09\uff0c\u4ee5\u4f7f\u5f97\u5230\u7684\u62ec\u53f7\u5b57\u7b26\u4e32\u6709\u6548\u3002<\/p>\n<p>\u4ece\u5f62\u5f0f\u4e0a\u8bb2\uff0c\u53ea\u6709\u6ee1\u8db3\u4e0b\u9762\u51e0\u70b9\u4e4b\u4e00\uff0c\u62ec\u53f7\u5b57\u7b26\u4e32\u624d\u662f\u6709\u6548\u7684\uff1a<\/p>\n<ul>\n<li>\u5b83\u662f\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\uff0c\u6216\u8005<\/li>\n<li>\u5b83\u53ef\u4ee5\u88ab\u5199\u6210&nbsp;<code>AB<\/code>&nbsp;\uff08<code>A<\/code>&nbsp;\u4e0e&nbsp;<code>B<\/code>&nbsp;\u8fde\u63a5\uff09, \u5176\u4e2d&nbsp;<code>A<\/code> \u548c&nbsp;<code>B<\/code>&nbsp;\u90fd\u662f\u6709\u6548\u5b57\u7b26\u4e32\uff0c\u6216\u8005<\/li>\n<li>\u5b83\u53ef\u4ee5\u88ab\u5199\u4f5c&nbsp;<code>(A)<\/code>\uff0c\u5176\u4e2d&nbsp;<code>A<\/code>&nbsp;\u662f\u6709\u6548\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u62ec\u53f7\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u4e3a\u4f7f\u7ed3\u679c\u5b57\u7b26\u4e32\u6709\u6548\u800c\u5fc5\u987b\u6dfb\u52a0\u7684\u6700\u5c11\u62ec\u53f7\u6570\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>\"())\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>1\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>\"(((\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>3\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>\"()\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>0\n\n<\/pre>\n<p><strong>\u793a\u4f8b 4\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>\"()))((\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>4<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ol>\n<li><code>S.length &lt;= 1000<\/code><\/li>\n<li><code>S<\/code> \u53ea\u5305\u542b&nbsp;<code>'('<\/code> \u548c&nbsp;<code>')'<\/code>&nbsp;\u5b57\u7b26\u3002<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6808\u3001 \u8d2a\u5fc3\u7b97\u6cd5\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a36 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8690.66% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.5 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.64% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u7edf\u8ba1\u672a\u5339\u914d\u7684\u5de6\u53f3\u62ec\u53f7\u3002\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def minAddToMakeValid(self, S: str) -&gt; int:\n        l, r = 0, 0     # \u5de6\u62ec\u53f7\uff0c\u53f3\u62ec\u53f7\u672a\u5339\u914d\u6570\u7edf\u8ba1\n        for s in S:\n            if s == '(':    # \u5982\u679c\u662f\u5de6\u62ec\u53f7\uff0c\u5219\u5de6\u62ec\u53f7+1\n                l += 1\n            if s == ')':    #\n                if l &lt; 1:   # \u5de6\u62ec\u53f7\u5339\u914d\u5b8c\u540e\uff0c\u672a\u5339\u914d\u7684\u53f3\u62ec\u53f7+1\n                    r += 1\n                else:\n                    l -= 1  # \u5982\u679c\u6709\u591a\u4f59\u7684\u5de6\u62ec\u53f7, \u5219\u6d88\u8017\u4e00\u4e2a\u5de6\u62ec\u53f7\n        return l+r  # \u8fd4\u56de\u672a\u5339\u914d\u7684\u5de6\u62ec\u53f7\u548c\u53f3\u62ec\u53f7\u7684\u603b\u548c<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u7531&nbsp;&#8216;(&#8216;&nbsp;\u548c&nbsp;&#8216;)&#8217;&nbsp;\u62ec\u53f7\u7ec4\u6210\u7684\u5b57\u7b26\u4e32 S\uff0c\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u6700\u5c11&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/547"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=547"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/547\/revisions"}],"predecessor-version":[{"id":548,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/547\/revisions\/548"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=547"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}