题解 | #字符串的排列#
字符串的排列
https://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7
#include <string> #include <vector> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param str string字符串 * @return string字符串vector */ //全排列字符串集合 set<string> res; //判断此字符串是否已经用过 int flag[10]; void find(string &str,string &temp) { if(temp.size()==str.size()) { res.insert(temp); return; } for(int i=0;i<str.size();i++) { //如果当前字符串已经被使用过,就不能再去加入组合 if(flag[i]==1)continue; //加入临时字符串 flag[i]=1; temp.push_back(str[i]); find(str,temp); //回溯 flag[i]=0; temp.pop_back(); } } vector<string> Permutation(string str) { // write code here string temp; find(str,temp); vector<string> strs; for(string str:res) { strs.push_back(str); } return strs; } };