题目描述
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
输入描述: 输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
看看牛客评论, 这题貌似挺多要挖的地方
class Solution {
public:
vector<string> vt;
set<string> my_set;
void Permutation(string str, unsigned int begin) {
if (begin == str.size()) {
my_set.insert(str);
return;
}
for (unsigned int i = begin; i < str.size(); i++) {
char temp = str[i];
str[i] = str[begin];
str[begin] = temp;
// vt.push_back(str);
Permutation(str, begin+1);
temp = str[i];
str[i] = str[begin];
str[begin] = temp;
}
}
vector<string> Permutation(string str) {
if (0 == str.size()) {
return vt;
}
Permutation(str, 0);
for (set<string>::iterator it = my_set.begin(); it != my_set.end(); it++) {
vt.push_back(*it);
}
return vt;
}
};
class Solution {
public:
vector<string> vt;
void Permutation(string str, unsigned int begin) {
if (begin == str.length()) {
vt.push_back(str);
return;
}
for (unsigned int i = begin; i != str.size(); i++) {
if (i != begin && str[i] == str[begin])
continue;
swap(str[i], str[begin]);
Permutation(str, begin+1);
swap(str[i], str[begin]);
}
}
vector<string> Permutation(string str) {
if (0 == str.size()) {
return vt;
}
Permutation(str, 0);
sort(vt.begin(),vt.end());
return vt;
}
};