解
全排列,深度优先遍历
固定首字符,求后面字符的全排列
首字符和依次与后面的数交换
vector<string>permutation(stringstr)
{
vector<string>res;
if(str.empty())
returnres;
dfs(str,str.size(),0,{},res);
sort(res.begin(),res.end());
returnres;
}
voiddfs(stringstr,intlength,intindex,stringout,vector<string>&res)
{
if(index==length)
{
res.push_back(out);
}
for(inti=index;i<length;i++)
{
if(i==index||str[i]!=str[index])
{
out.push_back(str[i]);
swap(str[i],str[index]);
help(str,length,index+1,out,res);
swap(str[index],str[i]);
out.pop_back();
}
}
}
评论0