题目描述
思路分析
暴搜 / 全排列next_permutation()枚举
代码实现
class Solution {
public:
vector<string> ans;
bool st[20];
unordered_map<string,int> hash;
vector<string> Permutation(string str) {
dfs(0,str,"");
return ans;
}
void dfs(int u,string s,string cur){
if(u==s.size()){
if(!hash[cur]){
ans.push_back(cur);
hash[cur]=1;
}
return;
}
for(int i=0;i<s.size();i++){
if(!st[i]){
st[i]=true;
dfs(u+1,s,cur+s[i]);
st[i]=false;
}
}
}
};
class Solution {
public:
vector<string> Permutation(string str) {
vector<string> ans;
sort(str.begin(),str.end());
do{
ans.push_back(str);
}while(next_permutation(str.begin(),str.end()));
return ans;
}
};