字符串的全排列

摘自《剑指offer》

题目要求很简单,输出一串字符串的全排列,例如:输入abc  输出  abc/acb/bac/bca/cba/cab

代码我是真没怎么看明白,可能这也是递归程序比较难调试的原因吧.

void Permutation(char *pstr,char *pBegin)
{
    if (*pBegin=='')
    {
        cout<<pstr<<endl;
    }
    else
    {
        for (char *pCh=pBegin;*pCh!='';pCh++)
        {
            char temp=*pCh;
            *pCh=*pBegin;
            *pBegin=temp;

            Permutation(pstr,pBegin+1);

            temp=*pCh;
            *pCh=*pBegin;
            *pBegin=temp;
        }
    }
}

void Permutation(char *pstr)
{
    if (pstr==NULL)
        return;
    Permutation(pstr,pstr);
}
原文地址:https://www.cnblogs.com/audi-car/p/4646513.html