{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "328698d1b0f24616a305fb22259b2146",
  "keywords": "栈,字符串,动态规划",
  "title": "最长有效括号",
  "desc": [
    {
      "content": "\n<p>给你一个只包含 <code>'('</code> 和 <code>')'</code> 的字符串，找出最长有效（格式正确且连续）括号子串的长度。</p><p> </p><div class=\"original__bRMd\"><div><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>s = \"(()\"<strong><br />输出：</strong>2<strong><br />解释：</strong>最长有效括号子串是 \"()\"</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>s = \")()())\"<strong><br />输出：</strong>4<strong><br />解释：</strong>最长有效括号子串是 \"()()\"</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>s = \"\"<strong><br />输出：</strong>0</pre><p> </p><p><strong>提示：</strong></p><ul>\t<li><code>0 <= s.length <= 3 * 10<sup>4</sup></code></li>\t<li><code>s[i]</code> 为 <code>'('</code> 或 <code>')'</code></li></ul></div></div>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint longestValidParentheses(string s)\n\t{\n\t\tstack<int> stk;\n\t\tint invalid = -1;\n\t\tint len = 0, max_len = 0;\n\t\tfor (int i = 0; i < s.length(); i++)\n\t\t{\n\t\t\tif (s[i] == '(')\n\t\t\t{\n\t\t\t\tstk.push(i);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (stk.empty())\n\t\t\t\t{\n\t\t\t\t\tinvalid = i;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tstk.pop();\n\t\t\t\t\tif (stk.empty())\n\t\t\t\t\t{\n\t\t\t\t\t\tmax_len = max(i - invalid, max_len);\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tmax_len = max(i - stk.top(), max_len);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn max_len;\n\t}\n};",
    "language": "cpp"
  },
  "node_id": "dailycode-a1becf27311a4e77a4c9cc59c8847eef",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600471008"
}