我是DFS的初学者,所以比较在意一些形式化的东西。现在我一步步得出那样的代码。
版本0: 学习permutation unique 的解法。
5 vector<vector<int> > permuteUnique(vector<int> &num) {
6 vector<vector<int> >result = vector<vector<int> >();
7 vector<int> tmp = vector<int>(); //intemediate result;
8 vector<bool> visit(num.size()); //state
9 int i, step;
10 if (num.size() == 0)
11 return result;
12 sort(num.begin(), num.end());
13 for(i = 0; i < num.size(); i++)
14 visit[i] = false;
15 dfs(result, tmp, num, visit, step);
16 return result;
17 }
18 void dfs(vector<vector<int> > &result, vector<int> tmp, vector<int> num, vector<bool> visit, int step) {
19 if (tmp.size() == num.size()) {
20 result.push_back(tmp);
21 return ;
22 }
23 int i;
24 for(i = 0; i < num.size(); i ++) {
25 // i-1 was visited last time, not this time, the result was same
26 if (visit[i] == true || (i != 0 && num[i] == num[i-1] && !visit[i-1]))
27 continue;
28 tmp.push_back(num[i]);
29 visit[i] = true;
30 dfs(result, tmp, num, visit, step+1);
31 tmp.pop_back();
32 visit[i] = false;
33 }
34 return;
35 }
这里 是DFS最基本的形式。
版本 一: 生成括号
4 vector<string> generateParenthesis(int n) {
5 vector<string> result;
6 string s = string(n, '(');
7 s.append(n,')');
8 string sol;
9 int left, right;
10 left = 0;
11 right = 0;
12 vector<bool> visit(2*n, false);
13 generate(sol, result, s, left, right, visit, n);
14 return result;
15 }
16 void generate(string sol, vector<string> &result, string s, int &left, int &right, vector<bool> visit, int n) {
17 if (left == n && right == n) {
18 result.push_back(sol);
19 return;
20 }
21 int i;
22 for (i = 0; i < s.size(); i ++) {
23 if (visit[i] == true || (i != 0 && s[i] == s[i-1] && !visit[i-1]))
24 continue;
25 if (left == right && s[i] == ')') {
26 continue;
27 }
28 if (s[i] == '(' && !visit[i]) {
29 visit[i] = true;
30 left = left +1;
31 sol.push_back(s[i]);
32 generate(sol, result, s, left, right, visit, n);
33 sol.pop_back();
34 left = left-1;
35 visit[i]= false;
36 }
37 else if (s[i] == ')' && !visit[i] && left > right) {
38 visit[i] = true;
39 right = right + 1;
40 sol.push_back(s[i]);
41 generate(sol, result, s, left, right, visit, n);
42 sol.pop_back();
43 right = right -1;
44 visit[i]= false;
45 }
46 }
47 }
版本2: 优化,循环中只有 2个元素, 所以没有必要搞个循环,只需要列举。
void getParenthesis(string &s, vector<string> &result, int &left, int &right, int n)
{
if(left==n && right==n)
{
result.push_back(s);
return;
}
if(left < n)
{
s.push_back('(');
left = left +1;
getParenthesis(s, result, left, right, n);
left = left -1;
s.pop_back();
}
if(right < left)
{
s.push_back(')');
right = right +1;
getParenthesis(s, result, left, right, n);
right = right -1;
s.pop_back();
}
}
vector<string> generateParenthesis(int n) {
vector<string> result;
if(n==0) return result;
string s;
int left, right;
left = 0;
right = 0;
getParenthesis(s, result, left, right, n);
return result;
}
版本三: 再次精简
{
if(left==n && right==n)
{
result.push_back(s);
return;
}
if(left < n)
{
getParenthesis(s + '(', result, left + 1, right, n);
}
if(right < left)
{
getParenthesis(s + ')', result , left, right + 1, n);
}
}
vector<string> generateParenthesis(int n) {
vector<string> result;
if(n==0) return result;
string s;
int left, right;
left = 0;
right = 0;
getParenthesis(s, result, left, right, n);
return result;
}