题解 | 字符串的排列
字符串的排列
https://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7
class Solution { private: vector<string> res; string path; void backtracing(string str,vector<int> & visited){ if(path.size() == str.size()) { res.push_back(path); } for(int i = 0 ; i < str.size() ; i++){ if(visited[i] == 1 || str[i] == str[i - 1] && visited[i-1] == 1 ) continue; visited[i] = 1; path += str[i]; backtracing(str, visited); path.pop_back(); visited[i] = 0; } } public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param str string字符串 * @return string字符串vector */ vector<string> Permutation(string str) { sort(str.begin(),str.end()); vector<int> visited(str.size(),0); backtracing(str, visited); return res; } };