{ "question_id": 20, "question_title": "有效的括号", "difficulty": "简单", "question_content": "<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>", "topic_link": "https://bbs.csdn.net/topics/600470114", "cpp": "#include <stack>\nchar ascii_tab[128];\nclass Solution\n{\npublic:\n\tbool isValid(string s)\n\t{\n\t\tif (s.size() == 0)\n\t\t\treturn true;\n\t\tstd::stack<char> st;\n\t\tascii_tab['('] = 11;\n\t\tascii_tab['{'] = 12;\n\t\tascii_tab['['] = 13;\n\t\tascii_tab[')'] = 21;\n\t\tascii_tab['}'] = 22;\n\t\tascii_tab[']'] = 23;\n\t\tfor (auto c : s)\n\t\t{\n\t\t\tchar n = ascii_tab[c];\n\t\t\tif (n < 20)\n\t\t\t\tst.push(n);\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (st.empty())\n\t\t\t\t\treturn false;\n\t\t\t\tif (n != st.top() + 10)\n\t\t\t\t\treturn false;\n\t\t\t\tst.pop();\n\t\t\t}\n\t\t}\n\t\tif (st.empty())\n\t\t\treturn true;\n\t\treturn false;\n\t}\n};", "java": "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}", "js": "/**\n * @param {string} s\n * @return {boolean}\n */\nvar isValid = function(s) {\n \tlet parentheses = [\n \t\t'(', '[', '{', \n \t\t')', ']', '}'\n \t];\n \tlet i=0;\n \tlet c;\n \tlet sum = [0,0,0]\n \tlet top = [];\n \twhile(i<s.length){\n \tc = s[i];\n \tfor(let j=0;j<=2;j++){\n \tif(c===parentheses[j]){\n \t\ttop.push(j);\n \t\tsum[j]++;\n \t}else if(c===parentheses[j+3]){\n \t\tif(top[top.length-1]!==j){\n \t\t\treturn false;\t\n \t\t}\n \t\ttop.pop();\n \t\tsum[j]--;\n \t}else {\n\n \t}\n \t}\n \ti++;\n \t} \n\n \tfor(let j=0;j<=2;j++){\n \t\tif(sum[j]!==0){\n \t\t\treturn false;\n \t\t}\n \t}\n \treturn true;\n};\n\nfunction main(){\n\tconst testCases = [\n\t\t[\"()\",true],\n\t\t[\"()[]{}\",true],\n\t\t[\"(]\",false],\n\t\t[\"([)]\",false],\n\t\t[\"{[]}\",true]\n\t];\n\tfor(const testCase of testCases){\n\t\tconst v = isValid(testCase[0]);\n\t\tif(v===testCase[1]){\n\t\t\tconsole.log(`[OK]`);\n\t\t}else{\n\t\t\tconsole.log(`[ERROR]`);\n\t\t}\n\t}\n}\n\nmain();\n", "python": "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 = \"()[]{}\"))", "status": 1, "keywords": "栈,字符串", "license": { "cpp": "csdn.net", "python": "csdn.net", "java": "csdn.net" }, "notebook": { "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/19/19_cpp.ipynb?type=file", "python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/19/19_python.ipynb?type=file", "java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/19/19_java.ipynb?type=file" }, "notebook_enable": 1 }