[22]. 括号生成
Posted Debroon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[22]. 括号生成相关的知识,希望对你有一定的参考价值。
[22]. 括号生成
题目
传送门:https://leetcode.cn/problems/generate-parentheses/
算法设计:回溯
括号问题可以分成俩类:
- 括号的合法性判断,主要是用栈
- 括号的合法生成,主要是回溯
搞懂回溯算法,最重要的是理解回溯框架。
回溯框架:
ans = []
void recall( 路径,选择列表 )
if 满足结束条件:
ans.add( 路径 )
return
for 选择 in 选择列表:
做选择
recall( 路径,选择列表 )
撤销选择
回溯框架,本质是遍历一颗决策树。
- 路径:已经做出的选择
- 选择列表:当前可以做的选择
- 结束条件:到了决策树底层,无法再做选择
核心在于 for 循环里面的递归,在递归之前做选择,在递归之后撤销选择。
- for 循环,如果可视化就是在遍历一颗 N 叉树
问题是,选择和撤销选择是在这颗树上做什么呢?
- 选择:是在这棵树上做前序遍历
- 撤销选择:是在这颗树上做后序遍历
所以,选择的可视化是在进入树的某一节点前执行,撤销选择的可视化是在离开树的某一节点后执行。
做选择:在进入节点前,从选择列表拿出一个选择,将它放入路径。
撤销选择:在离开节点后,从路径中拿出一个选择,将它恢复到选择列表中。
套用回溯框架,打印所有括号:
- 结束条件:当前位置 == 2 * n
- 选择列表:每个位置要么左括号,要么右括号 [‘(’, ‘)’]
int n;
void recall( int x, string& ans )
if( x == 2 * n )
print(ans);
return;
for c in ['(', ')']:
ans.push(c);
recall(x + 1, ans);
ans.pop(c);
不过,这题不是生成所有括号,而是生成所有合法括号。
那么合法的定义是什么?
- 左括号数量 = 右括号数量
- 必然是左括号在前,右括号在后
- 在添加左括号时,之前的左括号一定大于或等于右括号
- 在添加右括号时,之前的左括号数量必然大于右括号
- 组合长度等于 2n(n对括号,一对括号是2,等于2n)
对于 2n
个括号,必然有 n
个左括号、n
个右括号
这里把穷举位置 x
,改成用 l
记录还可以使用多少个左括号,用 r
记录还可以使用多少个右括号。
class Solution
vector<string> res; // 记录答案
public:
vector<string> generateParenthesis(int n)
if( n == 0 ) return res;
recall(n , n, ""); // 当前可用左括号和右括号数量为 n
return res;
void recall(int l, int r, string str)
if( l < 0 || r < 0 ) return; // 剪枝:数量小于0,不合法
if( r < l ) return; // 剪枝:左括号剩下的多,不合法
if( l == 0 && r == 0 ) // 所有括号恰好用完,得到一个合法括号
res.push_back(str);
return;
str.push_back('('); // 选择左括号
recall(l-1, r, str); // 进入下一层
str.pop_back(); // 撤销选择
str.push_back(')');
recall(l, r-1, str);
str.pop_back();
;
当 n = 2 时,下图是代码的可视化:
算法设计:空间换时间
回溯算法的时间复杂度太高了。
因为这题的输入数据 < 8,所以可以采用空间换时间策略。
class Solution
public:
vector<string> generateParenthesis(int n)
switch(n)
case 1:
return vector<string>"()";
case 2:
return vector<string>"()()","(())";
case 3:
return vector<string>"((()))","(()())","(())()","()(())","()()()";
case 4:
return vector<string>"(((())))","((()()))","((())())","((()))()","(()(()))","(()()())","(()())()","(())(())","(())()()","()((()))","()(()())","()(())()","()()(())","()()()()";
case 5:
return vector<string>"((((()))))","(((()())))","(((())()))","(((()))())","(((())))()","((()(())))","((()()()))","((()())())","((()()))()","((())(()))","((())()())","((())())()","((()))(())","((()))()()","(()((())))","(()(()()))","(()(())())","(()(()))()","(()()(()))","(()()()())","(()()())()","(()())(())","(()())()()","(())((()))","(())(()())","(())(())()","(())()(())","(())()()()","()(((())))","()((()()))","()((())())","()((()))()","()(()(()))","()(()()())","()(()())()","()(())(())","()(())()()","()()((()))","()()(()())","()()(())()","()()()(())","()()()()()";
case 6:
return vector<string>"(((((())))))","((((()()))))","((((())())))","((((()))()))","((((())))())","((((()))))()","(((()(()))))","(((()()())))","(((()())()))","(((()()))())","(((()())))()","(((())(())))","(((())()()))","(((())())())","(((())()))()","(((()))(()))","(((()))()())","(((()))())()","(((())))(())","(((())))()()","((()((()))))","((()(()())))","((()(())()))","((()(()))())","((()(())))()","((()()(())))","((()()()()))","((()()())())","((()()()))()","((()())(()))","((()())()())","((()())())()","((()()))(())","((()()))()()","((())((())))","((())(()()))","((())(())())","((())(()))()","((())()(()))","((())()()())","((())()())()","((())())(())","((())())()()","((()))((()))","((()))(()())","((()))(())()","((()))()(())","((()))()()()","(()(((()))))","(()((()())))","(()((())()))","(()((()))())","(()((())))()","(()(()(())))","(()(()()()))","(()(()())())","(()(()()))()","(()(())(()))","(()(())()())","(()(())())()","(()(()))(())","(()(()))()()","(()()((())))","(()()(()()))","(()()(())())","(()()(()))()","(()()()(()))","(()()()()())","(()()()())()","(()()())(())","(()()())()()","(()())((()))","(()())(()())","(()())(())()","(()())()(())","(()())()()()","(())(((())))","(())((()()))","(())((())())","(())((()))()","(())(()(()))","(())(()()())","(())(()())()","(())(())(())","(())(())()()","(())()((()))","(())()(()())","(())()(())()","(())()()(())","(())()()()()","()((((()))))","()(((()())))","()(((())()))","()(((()))())","()(((())))()","()((()(())))","()((()()()))","()((()())())","()((()()))()","()((())(()))","()((())()())","()((())())()","()((()))(())","()((()))()()","()(()((())))","()(()(()()))","()(()(())())","()(()(()))()","()(()()(()))","()(()()()())","()(()()())()","()(()())(())","()(()())()()","()(())((()))","()(())(()())","()(())(())()","()(())()(())","()(())()()()","()()(((())))","()()((()()))","()()((())())","()()((()))()","()()(()(()))","()()(()()())","()()(()())()","()()(())(())","()()(())()()","()()()((()))","()()()(()())","()()()(())()","()()()()(())","()()()()()()";
case 7:
return vector<string>"((((((()))))))","(((((()())))))","(((((())()))))","(((((()))())))","(((((())))()))","(((((()))))())","(((((())))))()","((((()(())))))","((((()()()))))","((((()())())))","((((()()))()))","((((()())))())","((((()()))))()","((((())(()))))","((((())()())))","((((())())()))","((((())()))())","((((())())))()","((((()))(())))","((((()))()()))","((((()))())())","((((()))()))()","((((())))(()))","((((())))()())","((((())))())()","((((()))))(())","((((()))))()()","(((()((())))))","(((()(()()))))","(((()(())())))","(((()(()))()))","(((()(())))())","(((()(()))))()","(((()()(()))))","(((()()()())))","(((()()())()))","(((()()()))())","(((()()())))()","(((()())(())))","(((()())()()))","(((()())())())","(((()())()))()","(((()()))(()))","(((()()))()())","(((()()))())()","(((()())))(())","(((()())))()()","(((())((()))))","(((())(()())))","(((())(())()))","(((())(()))())","(((())(())))()","(((())()(())))","(((())()()()))","(((())()())())","(((())()()))()","(((())())(()))","(((())())()())","(((())())())()","(((())()))(())","(((())()))()()","(((()))((())))","(((()))(()()))","(((()))(())())","(((()))(()))()","(((()))()(()))","(((()))()()())","(((()))()())()","(((()))())(())","(((()))())()()","(((())))((()))","(((())))(()())","(((())))(())()","(((())))()(())","(((())))()()()","((()(((())))))","((()((()()))))","((()((())())))","((()((()))()))","((()((())))())","((()((()))))()","((()(()(()))))","((()(()()())))","((()(()())()))","((()(()()))())","((()(()())))()","((()(())(())))","((()(())()()))","((()(())())())","((()(())()))()","((()(()))(()))","((()(()))()())","((()(()))())()","((()(())))(())","((()(())))()()","((()()((()))))","((()()(()())))","((()()(())()))","((()()(()))())","((()()(())))()","((()()()(())))","((()()()()()))","((()()()())())","((()()()()))()","((()()())(()))","((()()())()())","((()()())())()","((()()()))(())","((()()()))()()","((()())((())))","((()())(()()))","((()())(())())","((()())(()))()","((()())()(()))","((()())()()())","((()())()())()","((()())())(())","((()())())()()","((()()))((()))","((()()))(()())","((()()))(())()","((()()))()(())","((()()))()()()","((())(((()))))","((())((()())))","((())((())()))","((())((()))())","((())((())))()","((())(()(())))","((())(()()()))","((())(()())())","((())(()()))()","((())(())(()))","((())(())()())","((())(())())()","((())(()))(())","((())(()))()()","((())()((())))","((())()(()()))","((())()(())())","((())()(()))()","((())()()(()))","((())()()()())","((())()()())()","((())()())(())","((())()())()()","((())())((()))","((())())(()())","((())())(())()","((())())()(())","((())())()()()","((()))(((())))","((()))((()()))","((()))((())())","((()))((()))()","((()))(()(()))","((()))(()()())","((()))(()())()","((()))(())(())","((()))(())()()","((()))()((()))","((()))()(()())","((()))()(())()","((()))()()(())","((()))()()()()","(()((((())))))","(()(((()()))))","(()(((())())))","(()(((()))()))","(()(((())))())","(()(((()))))()","(()((()(()))))","(()((()()())))","(()((()())()))","(()((()()))())","(()((()())))()","(()((())(())))","(()((())()()))","(()((())())())","(()((())()))()","(()((()))(()))","(()((()))()())","(()((()))())()","(()((())))(())","(()((())))()()","(()(()((()))))","(()(()(()())))","(()(()(())()))","(()(()(()))())","(()(()(())))()","(()(()()(())))","(()(()()()()))","(()(()()())())","(()(()()()))()","(()(()())(()))","(()(()())()())","(()(()())())()","(()(()()))(())","(()(()()))()()","(()(())((())))","(()(())(()()))","(()(())(())())","(()(())(()))()","(()(())()(()))","(()(())()()())","(()(())()())()","(()(())())(())","(()(())())()()","(()(()))((()))","(()(()))(()())","(()(()))(())()","(()(()))()(())","(()(()))()()()","(()()(((()))))","(()()((()())))","(()()((())()))","(()()((()))())","(()()((())))()","(()()(()(())))","(()()(()()()))","(()()(()())())","(()()(()()))()","(()()(())(()))","(()()(())()())","(()()(())())()","(()()(()))(())","(()()(()))()()","(()()()((())))","(()()()(()()))","(()()()(())())","(()()()(()))()","(()()()()(()))","(()()()()()())","(()()()()())()","(()()()())(())","(()()()())()()","(()()())((()))","(()()())(()())","(()()())(())()","(()()())()(())","(()()())()()()","(()())(((())))","(()())((()()))","(()())((())())","(()())((()))()","(()())(()(()))","(()())(()()())","(()())(()())()","(()())(())(())","(()())(())()()","(()())()((()))","(()())()(()())","(()())()(())()","(()())()()(())","(()())()()()()","(())((((()))))","(())(((()())))","(())(((())()))","(())(((()))())","(())(((())))()","(())((()(())))","(())((()()()))","(())((()())())","(())((()()))()","(())((())(()))","(())((())()())","(())((())())()","(())((()))(())","(())((()))()()","(())(()((())))","(())(()(()()))","(())(()(())())","(())(()(()))()","(())(()()(()))","(())(()()()())","(())(()()())()","(())(()())(())","(())(()())()()","(())(())((()))","(())(())(()())","(())(())(())()","(())(())()(())","(())(())()()()","(())()(((())))","(())()((()()))","(())()((())())","(())()((()))()","(())()(()(()))","(())()(()()())","(())()(()())()","(())()(())(())","(())()(())()()","(())()()((()))","(())()()(()())","(())()()(())()","(())()()()(())","(())()()()()()","()(((((())))))","()((((()()))))","()((((())())))","()((((()))()))","()((((())))())","()((((()))))()","()(((()(()))))","()(((()()())))","()(((()())()))","()(((()()))())","()(((()())))()","()(((())(())))","()(((())()()))","()(((())())())","()(((())()))()","()(((()))(()))","()(((()))()())","()(((()))())()","()(((())))(())","()(((())))()()","()((()((()))))","()((()(()())))","()((()(())()))"LeetCode 22. 括号生成(Generate Parentheses)