输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
输入描述:
输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
void Permutation(vector<string> &res,int s,string str){
if(s==str.size()-1)
res.push_back(str);
unordered_set<char> temp; //记录同层访问查重
sort(str.begin()+s,str.end());
for(int i=s;i<str.size();i++)
{
if(temp.find(str[i])==temp.end())
{
temp.insert(str[i]);//用于 递归的去重
swap(str[i],str[s]);
Permutation(res,s+1,str);
swap(str[i],str[s]);
}
}
}
vector<string> Permutation(string str) {
vector<string> res;
Permutation(res,0,str);
return res;
}
https://blog.youkuaiyun.com/yanbao4070/article/details/80050726