一、题目描述:
二、思路分析:
int换成字符有啥区别
代码直接改下变量类型!
提交!
哦原来是考虑重复问题了
那么就vector -> set -> vector
三、AC 代码:
class Solution {
public:
vector<string> permutation(string s) {
vector<string> res;
vector<bool> used;
int n = s.size();
int de = 0;
for(int i = 0 ; i < n; i++){
used.push_back(false);
}
string path = "";
dfs(s, n, de , path, used, res);
set<string> st(res.begin(), res.end());
res.assign(st.begin(), st.end());
return res;
}
void dfs(string nums, int len, int depth, string path, vector<bool>& used, vector<string>& res){
if(len == depth){
res.push_back(path);
return ;
}
for(int i = 0; i < nums.size(); i++){
if(!used[i]){
// false 说明没人用过
path += nums[i];
used[i] = true;
dfs(nums, len, depth + 1, path, used, res);
used[i] = false;
path.erase(path.end() - 1);
}
}
return;
}
};
四、总结:
通过set的特性避免了重复的问题,这种解法到底是特性还是投机取巧呢?
本文正在参与「掘金 2021 春招闯关活动」, 点击查看 活动详情