solution.json 2.7 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "6b15cf2f03a14e478ddc2c2d135852e4",
  "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": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "class Solution {\n\tpublic boolean isValid(String s) {\n\t\tchar[] parentheses = { '(', '[', '{', ')', ']', '}' };\n\t\tint i = 0;\n\t\tchar c;\n\t\tint[] sum = { 0, 0, 0 };\n\t\tStack<Integer> top = new Stack<Integer>();\n\t\twhile (i < s.length()) {\n\t\t\tc = s.charAt(i);\n\t\t\tfor (int j = 0; j <= 2; j++) {\n\t\t\t\tif (c == parentheses[j]) {\n\t\t\t\t\ttop.push(j);\n\t\t\t\t\tsum[j]++;\n\t\t\t\t} else if (c == parentheses[j + 3]) {\n\t\t\t\t\tif (top.size() == 0 || top.peek() != j) {\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\ttop.pop();\n\t\t\t\t\tsum[j]--;\n\t\t\t\t} else {\n\t\t\t\t}\n\t\t\t}\n\t\t\ti++;\n\t\t}\n\t\tfor (int j = 0; j <= 2; j++) {\n\t\t\tif (sum[j] != 0) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t}\n}",
    "language": "java"
  },
  "node_id": "dailycode-d1b9ca19b0a24258833ae33eac8f8ee6",
  "license": "csdn.net",
  "created_at": 1637894161,
  "topic_link": "https://bbs.csdn.net/topics/600470114"
}