字符串全排列问题(递归算法)
2017-06-30 08:36
197 查看
#include <stdio.h> void swap(char array[],int src,int dst) { char temp = array[src]; array[src] = array[dst]; array[dst] = temp; } void Permutation(char array[],int from , int to ) { printf("start..from = %d,to = %d \n",from,to); int i=0; if(from == to) { for(i=0;i<=to;i++) { printf("%c",array[i]); } printf("\n"); } for(i=from;i<=to;i++) { swap(array,i,from); Permutation(array,from+1,to); swap(array,i,from); } } int main() { char str[] = "1234"; int len = sizeof(str)/sizeof(char); Permutation(str,0,len-2); }
以1234为例:
采用递归的思想:
1 -> 2 3 4 继续调用自身做n-1规模的全排列
2 -> 1 3 4 swap之后,继续调用自身做n-1规模的全排列
3 -> 2 1 4 swap之后,继续调用自身。。。。
4 -> 2 3 1 同上
注意:每次调用前保证1234的顺序不变,否则会有遗漏
相关文章推荐
- 求字符串全排列的递归算法(java程序)
- 求字符串全排列的递归算法
- 字符串全排列问题
- 有关字符串的全排列,组合 等问题
- 字符串全排列问题
- 字符串全排列问题
- 字符串全排列问题
- 字符串的全排列问题(一)——无重复出现字符的排列
- LeetCode—**字符串中数字的全排列问题Permutations
- 字符串全排列问题
- 递归算法之全排列问题
- 递归解决输出一个字符串的全排列问题(缺陷:没有考虑字符串中字符重复的问题)
- 字符串全排列问题
- 字符串的全排列问题
- 字符串的全排列【递归算法训练】
- 字符串的全排列问题
- 字符串 全排列生成问题
- 算法习题53:字符串全排列问题
- 使用C语言解决字符串全排列问题
- 字符串 全排列生成问题 (转)