-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path038_01-字符串的排列.cpp
More file actions
33 lines (32 loc) · 970 Bytes
/
038_01-字符串的排列.cpp
File metadata and controls
33 lines (32 loc) · 970 Bytes
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
class Solution {
public:
vector<string> Permutation(string str) {
//判断输入
if(str.length() == 0){
return result;
}
PermutationCore(str, 0);
//对结果进行排序
sort(result.begin(), result.end());
return result;
}
private:
void PermutationCore(string str, int begin){
//递归结束的条件:第一位和最后一位交换完成
if(begin == str.length()){
result.push_back(str);
return;
}
for(int i = begin; i < str.length(); i++){
//如果字符串相同,则不交换
if(i != begin && str[i] == str[begin]){
continue;
}
//位置交换
swap(str[begin], str[i]);
//递归调用,前面begin+1的位置不变,后面的字符串全排列
PermutationCore(str, begin + 1);
}
}
vector<string> result;
};