JZ-C-28
剑指offer第二十八题:字符串的排列
1 //============================================================================ 2 // Name : JZ-C-28.cpp 3 // Author : Laughing_Lz 4 // Version : 5 // Copyright : All Right Reserved 6 // Description : 字符串的排列 7 //============================================================================ 8 9 #include <iostream> 10 #include <stdio.h> 11 using namespace std; 12 13 void Permutation(char* pStr, char* pBegin); 14 void Permutation(char* pStr) { 15 if (pStr == NULL) { 16 return; 17 } 18 Permutation(pStr, pStr); 19 } 20 void Permutation(char* pStr, char* pBegin) { 21 if (*pBegin == '\0') { 22 printf("%s\n", pStr); 23 } else { 24 for (char* pCh = pBegin; *pCh != '\0'; pCh++) { 25 char temp = *pBegin; 26 *pBegin = *pCh; 27 *pCh = temp; 28 29 Permutation(pStr, pBegin + 1); 30 31 temp = *pBegin; 32 *pBegin = *pCh; 33 *pCh = temp; 34 } 35 } 36 37 } 38 // ====================测试代码==================== 39 void Test(char* pStr) { 40 if (pStr == NULL) 41 printf("Test for NULL begins:\n"); 42 else 43 printf("Test for %s begins:\n", pStr); 44 45 Permutation(pStr); 46 47 printf("\n"); 48 } 49 50 int main(int argc, char** argv) { 51 Test(NULL); 52 53 char string1[] = ""; 54 Test(string1); 55 56 char string2[] = "a"; 57 Test(string2); 58 59 char string3[] = "ab"; 60 Test(string3); 61 62 char string4[] = "abc"; 63 Test(string4); 64 65 return 0; 66 }
—————————————————————————————————————行走在人猿的并行线——Laughing_Lz