{ "type": "code_options", "author": "csdn.net", "source": "solution.md", "exercise_id": "57af5c6123504bd884409d603dd5f7f3", "keywords": "字符串,动态规划,回溯", "title": "括号生成", "desc": [ { "content": "\n

数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。

 

示例 1:

输入:n = 3
输出:
[\"((()))\",\"(()())\",\"(())()\",\"()(())\",\"()()()\"]

示例 2:

输入:n = 1
输出:
[\"()\"]

 

提示:

", "language": "markdown" } ], "answer": [ { "content": "", "language": "cpp" } ], "prepared": [ [ { "content": "", "language": "cpp" } ], [ { "content": "", "language": "cpp" } ], [ { "content": "", "language": "cpp" } ] ], "template": { "content": "class Solution\n{\npublic:\n\tvoid gen(string &p, int lc, int rc, vector &r, int n)\n\t{\n\t\tif (lc > n)\n\t\t\treturn;\n\t\tif (lc == n && rc == n)\n\t\t{\n\t\t\tr.push_back(p);\n\t\t\treturn;\n\t\t}\n\t\tp.push_back('(');\n\t\tlc++;\n\t\tgen(p, lc, rc, r, n);\n\t\tp.pop_back();\n\t\tlc--;\n\t\tif (lc > rc)\n\t\t{\n\t\t\tp.push_back(')');\n\t\t\trc++;\n\t\t\tgen(p, lc, rc, r, n);\n\t\t\tp.pop_back();\n\t\t\trc--;\n\t\t}\n\t}\n\tvector generateParenthesis(int n)\n\t{\n\t\tstring p;\n\t\tint lc = 0, rc = 0;\n\t\tvector r;\n\t\tgen(p, lc, rc, r, n);\n\t\treturn r;\n\t}\n};", "language": "cpp" }, "node_id": "dailycode-9399618c5b70447fa35668bbe36d2da8", "license": "csdn.net", "created_at": 1637894158, "topic_link": "https://bbs.csdn.net/topics/600470115" }