{"id":4151,"date":"2018-10-03T09:07:40","date_gmt":"2018-10-03T16:07:40","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4151"},"modified":"2018-10-03T19:15:46","modified_gmt":"2018-10-04T02:15:46","slug":"leetcode-32-longest-valid-parentheses","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/stack\/leetcode-32-longest-valid-parentheses\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 32. Longest Valid Parentheses"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a string containing just the characters\u00a0<code>'('<\/code>\u00a0and\u00a0<code>')'<\/code>, find the length of the longest valid (well-formed) parentheses substring.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> \"(()\"\r\n<strong>Output:<\/strong> 2\r\n<strong>Explanation:<\/strong> The longest valid parentheses substring is <code>\"()\"<\/code><\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input:<\/strong> \"<code>)()())<\/code>\" <strong>Output:<\/strong> 4 <strong>Explanation:<\/strong> The longest valid parentheses substring is <code>\"()()\"<\/code><\/pre>\n<h1><strong>Solution: Stack<\/strong><\/h1>\n<p>Use a stack to track the index of all unmatched open parentheses.<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  int longestValidParentheses(string s) {\r\n    stack&lt;int&gt; q;\r\n    int start = 0;\r\n    int ans = 0;\r\n    for (int i = 0;i &lt; s.length(); i++) {\r\n      if(s[i] == '(') {\r\n        q.push(i);\r\n      } else {\r\n        if (q.empty()) {\r\n          start = i + 1;\r\n        } else {\r\n          int index = q.top(); q.pop();\r\n          ans = max(ans, q.empty() ? i - start + 1 : i - q.top());          \r\n        }\r\n      }\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true \"># Author: Huahua\r\nclass Solution:\r\n  def longestValidParentheses(self, s):\r\n    q = []\r\n    start, ans = 0, 0\r\n    for i in range(len(s)):\r\n      if s[i] == '(':\r\n        q.append(i)\r\n        continue\r\n      if not q: \r\n        start = i + 1\r\n      else:\r\n        q.pop()\r\n        ans = max(ans, i - q[-1] if q else i - start + 1)\r\n    return ans<\/pre>\n<p>&nbsp;<\/p>\n<\/div><\/div>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/stack\/leetcode-20-valid-parentheses\/\">\u82b1\u82b1\u9171 LeetCode 20. Valid Parentheses<\/a><\/li>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-856-score-of-parentheses\/\">\u82b1\u82b1\u9171 LeetCode 856. Score of Parentheses<\/a><\/li>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-22-generate-parentheses\/\">\u82b1\u82b1\u9171 LeetCode 22. Generate Parentheses<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a string containing just the characters\u00a0&#8216;(&#8216;\u00a0and\u00a0&#8216;)&#8217;, find the length of the longest valid (well-formed) parentheses substring. Example 1: Input: &#8220;(()&#8221; Output: 2 Explanation:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[407],"tags":[217,180],"class_list":["post-4151","post","type-post","status-publish","format-standard","hentry","category-stack","tag-hard","tag-stack","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4151"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4151\/revisions"}],"predecessor-version":[{"id":4155,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4151\/revisions\/4155"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}