笔试题:给出一个函数来输出字符串的所有排列
2014-07-01 15:12
246 查看
题目:给出一个函数来输出一个字符串的所有排列。比如abc,需要输出abc,acb,bac,bca,cab,cba。
解答:这是一个很好的考察递归理解的题目。我们以字符串abc为例,来分析一下字符串的排列过程。首先我们固定第一个字符a,求后面字符串bc
的排列。我们把第一个字符a和后面的紧挨的字符b交换,得到bac,接着我们固定第一个字符b,求后面的字符串ac的排列。然后将c和第一个
位置的a交换,得到cab,我们固定c,求后面的字符串ab的排列,很显然我们在交换a和c的时候,首先要将b和a换回来,也就是说在求每个子串的排列之后,
我们应该将之前交换的字符串换回来。在这里我们需要维护两个字符串指针,一个pStr表示字符串的排列,一个pBegin表示需要固定字符的指针位置。
#include <iostream>
using namespace std;
void Permutation(char* pStr, char* pBegin);
/////////////////////////////////////////////////////////////////////////
// Get the permutation of a string,
// for example, input string abc, its permutation is
// abc acb bac bca cba cab
/////////////////////////////////////////////////////////////////////////
void Permutation(char* pStr)
{
Permutation(pStr, pStr);
}
/////////////////////////////////////////////////////////////////////////
// Print the permutation of a string,
// Input: pStr - input string
// pBegin - points to the begin char of string
// which we want to permutate in this recursion
/////////////////////////////////////////////////////////////////////////
void Permutation(char* pStr, char* pBegin)
{
if(!pStr || !pBegin)
return;
// if pBegin points to the end of string,
// this round of permutation is finished,
// print the permuted string
if( *pBegin == '\0')
{
printf("%s\n", pStr);
}
// otherwise, permute string
else
{
for(char* pCh = pBegin; *pCh != '\0'; ++ pCh)
{
// swap pCh and pBegin
char temp = *pCh;
*pCh = *pBegin;
*pBegin = temp;
Permutation(pStr, pBegin + 1);
// restore pCh and pBegin
temp = *pCh;
*pCh = *pBegin;
*pBegin = temp;
}
}
}
int main()
{
char strTemp[10] = "1234";
Permutation( strTemp );
system( "pause" );
return 0;
}
解答:这是一个很好的考察递归理解的题目。我们以字符串abc为例,来分析一下字符串的排列过程。首先我们固定第一个字符a,求后面字符串bc
的排列。我们把第一个字符a和后面的紧挨的字符b交换,得到bac,接着我们固定第一个字符b,求后面的字符串ac的排列。然后将c和第一个
位置的a交换,得到cab,我们固定c,求后面的字符串ab的排列,很显然我们在交换a和c的时候,首先要将b和a换回来,也就是说在求每个子串的排列之后,
我们应该将之前交换的字符串换回来。在这里我们需要维护两个字符串指针,一个pStr表示字符串的排列,一个pBegin表示需要固定字符的指针位置。
#include <iostream>
using namespace std;
void Permutation(char* pStr, char* pBegin);
/////////////////////////////////////////////////////////////////////////
// Get the permutation of a string,
// for example, input string abc, its permutation is
// abc acb bac bca cba cab
/////////////////////////////////////////////////////////////////////////
void Permutation(char* pStr)
{
Permutation(pStr, pStr);
}
/////////////////////////////////////////////////////////////////////////
// Print the permutation of a string,
// Input: pStr - input string
// pBegin - points to the begin char of string
// which we want to permutate in this recursion
/////////////////////////////////////////////////////////////////////////
void Permutation(char* pStr, char* pBegin)
{
if(!pStr || !pBegin)
return;
// if pBegin points to the end of string,
// this round of permutation is finished,
// print the permuted string
if( *pBegin == '\0')
{
printf("%s\n", pStr);
}
// otherwise, permute string
else
{
for(char* pCh = pBegin; *pCh != '\0'; ++ pCh)
{
// swap pCh and pBegin
char temp = *pCh;
*pCh = *pBegin;
*pBegin = temp;
Permutation(pStr, pBegin + 1);
// restore pCh and pBegin
temp = *pCh;
*pCh = *pBegin;
*pBegin = temp;
}
}
}
int main()
{
char strTemp[10] = "1234";
Permutation( strTemp );
system( "pause" );
return 0;
}
相关文章推荐
- 给出一个函数来输出一个字符串的所有排列
- 70 给出一个函数来输出一个字符串的所有排列
- 给出一个函数来输出一个字符串的所有排列
- 给出一个函数来输出一个字符串的所有排列
- 3、给出一个函数输出字符串的所有排列
- 给出一个函数来输出一个字符串的所有排列。
- 算法40(字符串的排列,给出一个函数来输出一个字符串的所有排列)
- 给出一个函数来输出一个字符串的所有排列。
- 微软等数据结构+算法面试100题(2)--给出一个函数来输出一个字符串的所有排列
- 给出一个函数来输出一个字符串的所有排列
- 给出一个函数来输出一个字符串的所有排列
- 写一个函数输出一个字符串中的所有排列
- 输出一个字符串所有排列。注意有重复字符
- 给定一个字符串,输出其所有的可能排列
- 写正确函数需要注意的地方:输入一个字符串,打印出该字符串中字符的所有排列
- 使用内建的isinstance函数将一个list中所有的字符串变成小写,非字符串直接输出
- 输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。 结果请按字母顺序输出。
- 用递归的思想写编写一个函数reverse_string(char * string)实现字符串反向输出和反向排列
- 编写函数fun(char *s),其功能是:把字符串中所有字符前移一个位置,串中的第1个字符移到最后。 例如,若输入字符串为:ABC123xyz ,则应输出字符串: BC123xyzA 。
- 已知一个字符串,输出它包含字符的所有排列(permutations)