原题链接:剑指 Offer 38. 字符串的排列
solution:
dfs+哈希去重
class Solution {
public:
vector<string> res;
vector<bool> sta;
unordered_set<string> hash; //去重
vector<string> permutation(string s) {
sta = vector<bool> (s.size(), false);
string path;
dfs(s, path, 0);
return res;
}
void dfs(string &s,string &path, int u) {
if(u == s.size()) {
if(hash.count(path)) return; //去重
res.push_back(path);
hash.insert(path);
return;
}
for(int i = 0;i < s.size();i++) {
if(sta[i]) continue;
path.push_back(s[i]);
sta[i] = true;
dfs(s, path, u + 1);
sta[i] = false;
path.pop_back();
}
}
};