题解 | #字符串的排列#
字符串的排列
http://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7
class Solution {
public:
vector<string> Permutation(string str) {
vector<string> res;
string path;
vector<bool> used(str.size(), false);
sort(str.begin(), str.end());
dfs(str, res, path, used);
return res;
}
void dfs(string str,vector<string>& res, string path, vector<bool>& used){
if(path.size()==str.size()){
res.push_back(path);
return;
}
for(int i=0; i<str.size(); i++){
if(i>0 && str[i]==str[i-1] && used[i-1]==true){
continue;
}
if(used[i]==false){
path.push_back(str[i]);
used[i] = true;
dfs(str, res, path, used);
used[i] = false;
path.pop_back();
}
}
}
};