{"id":511,"date":"2020-09-26T10:50:37","date_gmt":"2020-09-26T02:50:37","guid":{"rendered":"http:\/\/39.96.58.60\/?p=511"},"modified":"2022-10-18T16:39:03","modified_gmt":"2022-10-18T08:39:03","slug":"leetcode-32-%e6%9c%80%e9%95%bf%e6%9c%89%e6%95%88%e6%8b%ac%e5%8f%b7","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=511","title":{"rendered":"Leetcode 32. \u6700\u957f\u6709\u6548\u62ec\u53f7"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u53ea\u5305\u542b <code>'('<\/code>&nbsp;\u548c <code>')'<\/code>&nbsp;\u7684\u5b57\u7b26\u4e32\uff0c\u627e\u51fa\u6700\u957f\u7684\u5305\u542b\u6709\u6548\u62ec\u53f7\u7684\u5b50\u4e32\u7684\u957f\u5ea6\u3002<\/p>\n<p><strong>\u793a\u4f8b&nbsp;1:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> \"(()\"\n\n<strong>\u8f93\u51fa:<\/strong> 2\n\n<strong>\u89e3\u91ca:<\/strong> \u6700\u957f\u6709\u6548\u62ec\u53f7\u5b50\u4e32\u4e3a <code>\"()\"<\/code>\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> \"<code>)()())<\/code>\"\n\n<strong>\u8f93\u51fa:<\/strong> 4\n\n<strong>\u89e3\u91ca:<\/strong> \u6700\u957f\u6709\u6548\u62ec\u53f7\u5b50\u4e32\u4e3a <code>\"()()\"<\/code>\n\n<\/pre>\n<p>**\u96be\u5ea6**: Hard<\/p>\n<p>**\u6807\u7b7e**: \u5b57\u7b26\u4e32\u3001 \u52a8\u6001\u89c4\u5212\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\uff1a52 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8686.20% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a14.7 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.47% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u52a8\u6001\u89c4\u5212\n    ))()))(()()))((\n\n    \u5bf9\u4e8e i = ( \uff0cdp[i] = 0\n    \u5bf9\u4e8e i = ) \uff0c\u9700\u8981\u770b\u524d\u9762\u4e00\u4e2a\n        \u5982\u679c dp[i-1] \u4e3a0 \uff0c\u5219\u9700\u770bs[i-1]\n            \u82e5s[i-1] = ( ,\u5219\u5339\u914d\uff0c \u5f53\u524d\u4e0e\u524d\u4e00\u4e2a\u62ec\u53f7\u5339\u914d\uff0c\u957f\u5ea6\u4e3a2\uff0c \u7136\u540e\u8fd8\u9700\u8981\u770b i-2 \u662f\u5426\u662f\u5339\u914d\u7684\u62ec\u53f7\n                dp[i] = dp[i-2] + 2\n            \u5426\u5219\n                dp[i] = 0\n        \u5982\u679c dp[i-1] \u4e0d\u4e3a0\uff0c\u5219\u524d\u9762\u5b58\u5728\u5339\u914d\u7684\u62ec\u53f7\uff0c\u6b64\u65f6\uff0c\u9700\u67e5\u770b s[i-1-dp[i-1]]\u5904\u5b57\u7b26\uff0c\u4e5f\u5c31\u662f\u524d\u9762\u5339\u914d\u597d\u7684\u5b57\u7b26\u7684\u524d\u4e00\u4e2a\u662f\u5f88\u4e48\u62ec\u53f7\n            \u82e5s[i-1-dp[i-1]] = (, \u5219\u5339\u914d\uff0c\n                dp[i] = dp[i-2-dp[i-1]] + 2 + dp[i - 1]\n            \u5426\u5219\n                dp[i] = 0\n\n    \u5728\u4e0a\u8ff0\u8fc7\u7a0b\u4e2d\uff0c\u9700\u5224\u65ad \u5217\u8868\u7d22\u5f15\u662f\u5426&lt;0\n\"\"\"\nclass Solution:\n    def longestValidParentheses(self, s: str) -&gt; int:\n        n = len(s)\n        if n == 0:\n            return 0\n\n        dp = [[] for _ in range(n)]\n        dp[0] = 0\n\n        for i in range(1, n):\n            if s[i] == '(':\n                dp[i] = 0\n            else:\n                if dp[i - 1] == 0:  # \u524d\u4e00\u4e2a\u5b57\u7b26\u662f ( \u6216 )\n                    if s[i-1] == '(':   # \u524d\u4e00\u4e2a\u5b57\u7b26\u662f (\n                        if i - 2 &gt; 0:   # \u8fd9\u91cc\u9700\u8981\u8fdb\u884c\u7d22\u5f15\u5224\u65ad\n                            dp[i] = 2 + dp[i - 1 - 1]\n                        else:\n                            dp[i] = 2\n                    else:           # \u524d\u4e00\u4e2a\u5b57\u7b26\u662f ), \u4e0d\u5339\u914d\n                        dp[i] = 0\n                else:\n                    if i - 1 - dp[i - 1] &gt;= 0:  # \u7d22\u5f15\u5224\u65ad\n                        if s[i - 1 - dp[i - 1]] == '(': # \u5339\u914d\u597d\u7684\u5b57\u7b26\u4e32\u524d\u4e00\u4e2a\u5b57\u7b26\u662f (\n                            if i - 2 - dp[i - 1] &gt; 0:\n                                dp[i] = dp[i - 1] + 2 + dp[i - 1 - dp[i - 1] - 1]\n                            else:\n                                dp[i] = dp[i - 1] + 2\n                        else:                           # \u5339\u914d\u597d\u7684\u5b57\u7b26\u4e32\u524d\u4e00\u4e2a\u5b57\u7b26\u662f )\n                            dp[i] = 0\n                    else:\n                        dp[i] = 0\n        return max(dp)\n\n\"\"\"\n\u53e6\u9644\u4e00\u4efd\u7cbe\u7b80\u540e\u7684\u4ee3\u7801\n\"\"\"\nclass Solution:\n    def longestValidParentheses(self, s: str) -&gt; int:\n        n = len(s)\n        if n == 0:\n            return 0\n\n        dp = [[] for _ in range(n)]\n        dp[0] = 0\n\n        for i in range(1, n):\n            if s[i] == ')' and i - 1 - dp[i - 1] &gt;= 0 and s[i - 1 - dp[i - 1]] == '(':\n                if i - 2 - dp[i - 1] &gt; 0:\n                    dp[i] = dp[i - 1] + 2 + dp[i - 1 - dp[i - 1] - 1]\n                else:\n                    dp[i] = dp[i - 1] + 2\n            else:\n                dp[i] = 0\n        return max(dp)<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u53ea\u5305\u542b &#8216;(&#8216;&nbsp;\u548c &#8216;)&#8217;&nbsp;\u7684\u5b57\u7b26\u4e32\uff0c\u627e\u51fa\u6700\u957f\u7684\u5305\u542b\u6709\u6548\u62ec\u53f7\u7684\u5b50\u4e32\u7684\u957f\u5ea6\u3002 \u793a\u4f8b&#038;&#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\/511"}],"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=511"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/511\/revisions"}],"predecessor-version":[{"id":512,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/511\/revisions\/512"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=511"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}