码迷,mamicode.com
首页 > 其他好文 > 详细

JZ-C-28

时间:2016-06-16 22:55:20      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

 剑指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 }

 

JZ-C-28

标签:

原文地址:http://www.cnblogs.com/Laughing-Lz/p/5592246.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!