/*************************************************************** 题目:输入一个字符串,打印出该字符串中字符的所有排列。例如输入 字符串abc,则打印出由a、b、c所能排列出来的所有字符串abc、acb、 bac、cab和cba。 ***************************************************************/ #include<stdio.h> void stringPermutation(char* pStr, char* pBegin); void stringPermutation(char* pStr) { if(pStr == NULL) return; stringPermutation(pStr,pStr); } void stringPermutation(char* pStr, char* pBegin) { if(*pBegin == ‘\0‘) //递归到字符串末尾 { printf("%s\n",pStr); } else { for(char* pCh=pBegin; *pCh != ‘\0‘; ++pCh) { char temp = *pCh; //交换子字符串第一个字符 *pCh = *pBegin; *pBegin = temp; stringPermutation(pStr,pBegin+1); temp = *pCh; //将子字符串第一个字符交换回来 *pCh = *pBegin; *pBegin = temp; } } } void test() { char pStr[] = "abc"; //注意这里不能写成char* pStr = "abc"; stringPermutation(pStr); } int main() { test(); return 0; }
void AllSubstring3(const char *str,char *arr) { //传入的arr的长度为len+1,最后一个位置保存‘\0‘ int i,j; unsigned int len = strlen(str); strcpy(arr,str); for(i=len-1;i>=0;i--) { arr[i+1] = ‘\0‘; for(j=i;j>=0;j--) printf("%s\t",&arr[j]); printf("\n"); } }
总结:
如果面试题是按照一定要求摆放若干个数字,我们可以先求出这些数字
的所有排列,然后再一一判断每个排列是不是满足题目给定的要求。
==参考剑指offer 和http://blog.csdn.net/ns_code/article/details/21043665
原文:http://blog.csdn.net/walkerkalr/article/details/21229341