class Solution {
void backtrack(vector& ans, string& cur, int open, int close, int n) {
if (cur.size() == n * 2) {
ans.push_back(cur);
return;
}
if (open < n) {
cur.push_back('(');
backtrack(ans, cur, open + 1, close, n);
cur.pop_back();
}
if (close < open) {
cur.push_back(')');
backtrack(ans, cur, open, close + 1, n);
cur.pop_back();
}
}
public:
vector generateParenthesis(int n) {
vector result;
string current;
backtrack(result, current, 0, 0, n);
return result;
}
};
问:来一个认真负责的大佬,详细列举举例一下回溯的过程,我一直不停的推导void backtrack,例如n=3,我只得到了((()))一个结果,希望详细一点
力扣上的原题的官方解析,回溯法,来一个认真负责的大佬
括号生成,力扣上原题,有一些疑惑
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- togolife 2021-12-27 17:38关注
#include <vector> #include <string> #include <iostream> using namespace std; class Solution { void backtrack(vector<string> &ans, string &cur, int open, int close, int n) { if (cur.size() == n * 2) { ans.push_back(cur); return; } if (open < n) { cur.push_back('('); cout << cur << endl; backtrack(ans, cur, open + 1, close, n); cur.pop_back(); cout << cur << endl; } if (close < open) { cur.push_back(')'); cout << cur << endl; backtrack(ans, cur, open, close + 1, n); cur.pop_back(); cout << cur << endl; } } public: vector<string> generateParenthesis(int n) { vector<string> result; string current; backtrack(result, current, 0, 0, n); return result; } }; int main(void) { Solution su; vector<string> r = su.generateParenthesis(2); cout << "result is: " << endl; for (int i = 0; i < r.size(); ++i) { cout << r[i] << endl; } return 0; }
打印每个步骤看一下:
以n=2为例子:
1.
current = ""
backtrack(result, current, 0, 0, 2);2.
open = 0, close = 0
current = "("
backtrack(result, current, 1, 0, 2); --> 进入if(open < n)内的backtrack3.
open = 1, close = 0
current = "(("
backtrack(result, current, 2, 0, 2); --> 进入if(open < n)内的backtrack4.
open = 2, close = 0
current = "(()"
backtrack(ans, cur, 2, 1, 2); --> 进入if(close < open)内的backtrack5.
open = 2, close = 1
current = "(())"
backtrack(ans, cur, 2, 2, 2); --> 进入if(close < open)内的backtrack6.
open = 2, close = 2
此时current.size() == 4
执行 ans.push_back(cur); --> 得到第一个合格字符串"(())"继续执行第5步
if (close < open) { cur.push_back(')'); backtrack(ans, cur, open, close + 1, n); cur.pop_back(); }
current = "(()"
继续执行第4步
if (close < open) { cur.push_back(')'); backtrack(ans, cur, open, close + 1, n); cur.pop_back(); }
current = "(("
继续执行第3步
if (open < n) { cur.push_back('('); backtrack(ans, cur, open + 1, close, n); cur.pop_back(); }
current = "("
并且第3步中: open = 1, close = 0, close < open
执行cur.push_back(')'); backtrack(ans, cur, open, close + 1, n);
current = "()"
backtrack(ans, cur, 1, 1, 2) --> 进入if(close < open)内的backtrack10.
open = 1, close = 1
current = "()("
backtrack(ans, cur, 2, 1, 2); --> 进入if(open < n)内的backtrack11.
open = 2, close = 1
current = "()()"
backtrack(ans, cur, 2, 2, 2) --> 进入if(close < open)内的backtrack13.
open = 2, close = 2
此时current.size() == 4
执行 ans.push_back(cur); --> 得到第二个合格字符串"()()"本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 PointNet++的onnx模型只能使用一次
- ¥20 西南科技大学数字信号处理
- ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
- ¥30 STM32 INMP441无法读取数据
- ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
- ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
- ¥15 用visualstudio2022创建vue项目后无法启动
- ¥15 x趋于0时tanx-sinx极限可以拆开算吗
- ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
- ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。