#include <vector>
class Solution {
public:
set<string> ans; //采用集合的方式,自动去掉重复的元素;
string temp = "";
vector<string> generatePermutation(string s) {
if(s == "")return {""};
dp(s, 0);
return vector<string>(ans.begin(), ans.end()); //将set转换成vector,注意构造方式
}
void dp(string &s, int index){
if(index == s.size()){
ans.insert(temp);
return;
}
temp.push_back(s[index]);
dp(s, index+1);
temp.pop_back();
dp(s, index+1);
return;
}
};

京公网安备 11010502036488号