defpermute(str,prefix):
iflen(str)==0:
printprefix,
return
permute(str[1:],prefix)
permute(str[1:],prefix+str[0])
permute('123','')
这是解答的结果,和上面二叉树来表示完全一致
322311312123
由于用Python的人并不是很多,所以我也用C写了个程序作为参考
#include
#include
#defineBUFFER_SIZE255
usingnamespacestd;
voidpermute(char*src,char*prefix)
{
charbuf[BUFFER_SIZE];
intlen=strlen(prefix);
if(strlen(src)==0)
{
cout