-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path38_string_Permutation.cpp
More file actions
133 lines (101 loc) · 2.85 KB
/
38_string_Permutation.cpp
File metadata and controls
133 lines (101 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// Created by mark on 2019/7/18.
// Copyright © 2019年 mark. All rights reserved.
//
/*
说明:
1. 问题:38. 全排列问题
2. 思路:递归,固定一个,然后交换
*/
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <assert.h>
#include <cstdio>
#include <fstream>
using namespace std;
/*
// 原书方法:指针用的很好,要多理解啊
void PermutationRecursive(char* pStr, char* pBegin);
// 全排列字符串
void Permutation(char* pStr)
{
if(pStr == nullptr)
return;
PermutationRecursive(pStr, pStr);
}
// 递归交换字符
void PermutationRecursive(char* pStr, char* pBegin)
{
if(*pBegin == '\0')
printf("%s\n", pStr);
else
{
for(char* pCh = pBegin; *pCh != '\0'; ++pCh)
{
char temp = *pCh; // 这里pBegin,pCh用于向后递归,交换最后两个字符
*pCh = *pBegin;
*pBegin = temp;
PermutationRecursive(pStr, pBegin+1); // 向后递归,直到最后两个字符,交换
temp = *pCh; // 用于递归返回后,把交换的字符放回在交换回来
*pCh = *pBegin;
*pBegin = temp;
}
}
}
*/
// 参考网上代码
bool isDuplicate(string& str, int begin, int end);
void PermutationRecursion(string str, int begin, vector<string> res);
vector<string> Permutation(string str, vector<string> res)
{
res.clear();
if(str.empty() == true)
return res;
PermutationRecursion(str,0,res);
sort(res.begin(), res.end());
return res;
}
void PermutationRecursion(string str, int begin, vector<string> res)
{
if(str[begin] == '\0')
{
res.push_back(str);
cout << str << endl;
}
else
{
for(int i = begin; str[i] != '\0'; ++i)
{
if(!isDuplicate(str,begin,i))
{
swap(str[i], str[begin]); // 递归到最后,返回递归的时候,开始交换最后两个字符
//cout <<"swap " <<str[i] <<"(" <<i <<")" <<" and " <<str[begin] <<"(" <<begin <<")" <<endl;
PermutationRecursion(str, begin + 1, res);
swap(str[i], str[begin]); // 输出str后,再交换回来
}
}
}
}
// 判断一个字符串,是否有跟最后一个字符重复的元素
bool isDuplicate(string& str, int begin, int end)
{
for(int i = begin; i < end; ++i)
{
if(str[i] == str[end])
return true;
}
return false;
}
int main(){
vector<string> res;
char str[] = "abc";
Permutation(str, res);
// 迭代器遍历vector
for(vector<string>::iterator iter = res.begin(); iter != res.end(); ++iter)
cout << *iter << endl;
return 0;
}