剑指offer之字符串的全排列

举报
chenyu 发表于 2021/07/26 23:38:50 2021/07/26
【摘要】 1 问题 求字符串的全排列,比如字符串abc,它的全排列如下 abc, acb, bac, bca, cad, cba             2 思路 我们先固定第一个字符,这里的第一个字符肯定是这个字符串里面字符串的全子集(不包含重复),abc字符串,我们a和a交换,这里也就是固定a字符的,然...

1 问题

求字符串的全排列,比如字符串abc,它的全排列如下

abc, acb, bac, bca, cad, cba
 

 

 

 

 

 

 

2 思路

我们先固定第一个字符,这里的第一个字符肯定是这个字符串里面字符串的全子集(不包含重复),abc字符串,我们a和a交换,这里也就是固定a字符的,然后我们只需要求出后面bc字符的全排列,这个时候我们可以用递归,我们依然b和b进行交换,c和c进行交换,到了末尾我们递归就结束,记得return.我们可以得出bc全排列的一种既bc,然后我们b和c需要交换位置,变成了cb,然后我们b和b进行了一次交换,然后我们可以得到bc全排列的一种既cb,所以固定a字符,我们可以得到全排列字符串abc acb,所以依次类推,我们可以固定b字符和c字符求出所以字符串的全排列

 

 

 

 

 

3 代码实现

c++版本


  
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6. class Permutation{
  7. private:
  8. void permutation(string str, int begin);
  9. public:
  10. vector<string> permutation(string str);
  11. vector<string> result;
  12. };
  13. vector<string> Permutation::permutation(string str)
  14. {
  15. if (str.length() == 0)
  16. {
  17. return result;
  18. }
  19. permutation(str, 0);
  20. sort(result.begin(), result.end());
  21. return result;
  22. }
  23. void Permutation::permutation(string str, int begin)
  24. {
  25. if (begin == str.length())
  26. {
  27. result.push_back(str);
  28. return;
  29. }
  30. for (int i = begin; i < str.length(); ++i)
  31. {
  32. if (i != begin && str[i] == str[begin])
  33. continue;
  34. //这里开始交换第一个元素和后面的元素,也就是起到固定的作用
  35. swap(str[begin], str[i]);
  36. //递归后面的子字符串
  37. permutation(str, begin + 1);
  38. //交换了要记得还原
  39. swap(str[i], str[begin]);
  40. }
  41. }
  42. int main()
  43. {
  44. Permutation h;
  45. h.permutation("abc");
  46. vector<string> result = h.result;
  47. for(int i = 0; i < result.size(); ++i)
  48. {
  49. std::cout << result[i] << std::endl;
  50. }
  51. return 0;
  52. }

c版本


  
  1. #include <stdio.h>
  2. void permutation(char *str);
  3. void permutation_core(char *str, char *begin);
  4. void permutation(char *str)
  5. {
  6. if (str == NULL)
  7. {
  8. printf("str is NULL\n");
  9. return;
  10. }
  11. permutation_core(str, str);
  12. }
  13. void permutation_core(char *str, char *begin)
  14. {
  15. if (*begin == '\0')
  16. {
  17. printf("%s\n", str);
  18. }
  19. else
  20. {
  21. for (char *ch = begin; *ch != '\0'; ++ch)
  22. {
  23. char temp = *ch;
  24. *ch = *begin;
  25. *begin = temp;
  26. permutation_core(str, begin + 1);
  27. temp = *ch;
  28. *ch = *begin;
  29. *begin = temp;
  30. }
  31. }
  32. }
  33. int main()
  34. {
  35. //这里不能用char *str = "adc",因为我们需要交换字符串里面的字符,需要操作栈内存
  36. char str[] = "abc";
  37. permutation(str);
  38. return 0;
  39. }

这样写,如果有重复的字符就有问题,比如ada,我们优化如下,去掉重


  
  1. #include <stdio.h>
  2. #include <string.h>
  3. void permutation(char *str);
  4. void swap(char *str, int start, int end);
  5. void permutation_core(char *str, int begin, int end);
  6. void permutation(char *str)
  7. {
  8. if (str == NULL)
  9. {
  10. printf("str is NULL\n");
  11. return;
  12. }
  13. int len = strlen(str);
  14. printf("len is %d\n", len);
  15. permutation_core(str, 0, len);
  16. }
  17. void permutation_core(char *str, int begin, int end)
  18. {
  19. if (begin == end)
  20. {
  21. printf("%s\n", str);
  22. }
  23. else
  24. {
  25. for (int i = begin; i < end; ++i)
  26. {
  27. if (i != begin && str[i] == str[begin])
  28. continue;
  29. swap(str, i, begin);
  30. permutation_core(str, begin + 1, end);
  31. swap(str, i, begin);
  32. }
  33. }
  34. }
  35. void swap(char str[], int start, int end)
  36. {
  37. char ch = str[start];
  38. str[start] = str[end];
  39. str[end] = ch;
  40. }
  41. int main()
  42. {
  43. //这里不能用char *str = "adc",因为我们需要交换字符串里面的字符,需要操作栈内存
  44. char str[] = "abc";
  45. permutation(str);
  46. return 0;
  47. }

 

 

 

4 运行结果

c++版本运行结果


  
  1. abc
  2. acb
  3. bac
  4. bca
  5. cab
  6. cba

c版本运行结果


  
  1. abc
  2. acb
  3. bac
  4. bca
  5. cba
  6. cab

 

文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。

原文链接:chenyu.blog.csdn.net/article/details/90745503

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。