{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "b32f322d76c744d8b7b99c4c00710077",
  "keywords": "栈,字符串",
  "title": "有效的括号",
  "desc": [
    {
      "content": "\n<p>给定一个只包括 <code>'('</code>，<code>')'</code>，<code>'{'</code>，<code>'}'</code>，<code>'['</code>，<code>']'</code> 的字符串 <code>s</code> ，判断字符串是否有效。</p><p>有效字符串需满足：</p><ol>\t<li>左括号必须用相同类型的右括号闭合。</li>\t<li>左括号必须以正确的顺序闭合。</li></ol><p> </p><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>s = \"()\"<strong><br />输出：</strong>true</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>s = \"()[]{}\"<strong><br />输出：</strong>true</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>s = \"(]\"<strong><br />输出：</strong>false</pre><p><strong>示例 4：</strong></p><pre><strong>输入：</strong>s = \"([)]\"<strong><br />输出：</strong>false</pre><p><strong>示例 5：</strong></p><pre><strong>输入：</strong>s = \"{[]}\"<strong><br />输出：</strong>true</pre><p> </p><p><strong>提示：</strong></p><ul>\t<li><code>1 <= s.length <= 10<sup>4</sup></code></li>\t<li><code>s</code> 仅由括号 <code>'()[]{}'</code> 组成</li></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "python"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ]
  ],
  "template": {
    "content": "class Solution:\n\tdef isValid(self, s: str) -> bool:\n\t\tparentheses = [\n\t\t\t'(', '[', '{',\n\t\t\t')', ']', '}'\n\t\t]\n\t\ti = 0\n\t\tsum = [0, 0, 0]\n\t\ttop = []\n\t\twhile i < len(s):\n\t\t\tc = s[i]\n\t\t\tj = 0\n\t\t\twhile j <= 2:\n\t\t\t\tif c == parentheses[j]:\n\t\t\t\t\ttop.append(j)\n\t\t\t\t\tsum[j] += 1\n\t\t\t\telif c == parentheses[j+3]:\n\t\t\t\t\tif len(top) == 0 or top[len(top)-1] != j:\n\t\t\t\t\t\treturn False\n\t\t\t\t\ttop.pop()\n\t\t\t\t\tsum[j] -= 1\n\t\t\t\tj += 1\n\t\t\ti += 1\n\t\tif sum[0] != 0 or sum[1] != 0 or sum[2] != 0:\n\t\t\treturn False\n\t\telse:\n\t\t\treturn True\n# %%\ns = Solution()\nprint(s.isValid(s = \"()[]{}\"))",
    "language": "python"
  },
  "node_id": "dailycode-c4157a6e02994060aa622e2fb33eda35",
  "license": "csdn.net",
  "created_at": 1637894161,
  "topic_link": "https://bbs.csdn.net/topics/600470114"
}