//采用DFS深度搜索得方式进行求解 是一个不错的思路
#define _CTR_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include<vector>
using namespace std;
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> res;
int lc = 0, rc = 0;
string path = "";
dfs(res,path, n, lc, rc);
return res;
}
void dfs(vector<string>& res, string path, int n, int lc, int rc) {
if (rc > lc || lc > n || rc > n) return;
if (lc == rc && lc == n) {
res.push_back(path);
return;
}
dfs(res, path + '(', n, lc + 1, rc);
dfs(res, path + ')', n, lc, rc + 1);
}
};
#if 0
int main()
{
Solution s1;
vector<string> m;
m = s1.generateParenthesis(3);
for (int i = 0; i < m.size(); i++)
{
cout << m[i] << endl;
}
system("pause");
return 0;
}
#endif
括号问题的解题思路
最新推荐文章于 2024-04-01 02:04:53 发布