输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
class Solution {
public:
vector<string> Permutation(string str) {
if (str.empty())
return ans;
chang(str,0,str.size());
sort(ans.begin(),ans.end());
auto it=unique(ans.begin(),ans.end());
ans.erase(it,ans.end());
return ans;
}
void chang(string &str,int start ,int len){
if (start ==len)
ans.push_back(str);
for (int i=start;i<len;i++){
swap(str[start],str[i]);
chang(str,start+1,len);
swap(str[start],str[i]);
}
}
vector< string> ans;
};
原文:https://www.cnblogs.com/hrnn/p/13416259.html