题目描述
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
输入描述:
输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
code
使用C++全排列next_permutation
//AC
class Solution {
public:
vector<string> Permutation(string str) {
//1、string转char用next_permutation
//2、string直接用permutation √
if(str=="") //判断输入字符串为空时
return {};//返回空集合
vector<string> ans;
sort(str.begin(),str.end());//先进行升序排列
do{
ans.push_back(str);
}while(next_permutation(str.begin(),str.end()));
return ans;
}
};
补充:使用字符数组char[]输入时
class Solution {
public:
vector<string> Permutation(string str) {
char ch[10];
cin>>ch;
sort(ch,ch+strlen(ch));//先将char[]进行先序排序
do{
cout<<ch<<endl;
}while(next_permutation(ch,ch+strlen(ch)));
}
};