字符串的排序
来源:互联网 发布:股票看图软件 编辑:程序博客网 时间:2024/06/05 21:21
题目:输入一个字符串,打印出该字符串中字符的所有排序
《剑指offer》解
void Premutation(char *pStr){if (pStr == NULL)return;Premutation(pStr, pStr);}void Premutation(char *pStr, char *pBegin){if (*pBegin == '\0')cout << pStr << endl;else{for (char *pch = pBegin; *pch != '\0'; ++pch){char temp = *pch;*pch = *pBegin;*pBegin = temp;Premutation(pStr, pBegin + 1);temp = *pch;*pch = *pBegin;*pBegin = temp;}}}
注:这种解法没有考虑,字符串中有相同的字符
在这中方法上进行修改——考虑字符串中可能出现相同的字符
#include<iostream>using namespace std;void Premutation(char *pStr);void Premutation(char *pStr, char *pBegin);void QuickSort(char *pStr);void QuickSort(char *pStr,int low,int hight);// 字符串排序 可以对字符串中有相同字符进行正确处理void Premutation(char *pStr){if (pStr == NULL)return;QuickSort(pStr);//对字符串进行排序Premutation(pStr, pStr);}void Premutation(char *pStr, char *pBegin){if (*pBegin == '\0')cout << pStr << endl;else{for (char *pch = pBegin; *pch != '\0'; ++pch){if (pBegin != pch && *pBegin == *pch)continue;char temp = *pch;*pch = *pBegin;*pBegin = temp;Premutation(pStr, pBegin + 1);temp = *pch;*pch = *pBegin;*pBegin = temp;}}}//快速排序void QuickSort(char *pStr){if (*pStr == '\0')return;QuickSort(pStr, 0, strlen(pStr)-1);}void QuickSort(char *pStr, int low, int hight){if (low >= hight)return;int first = low, last = hight;char key = pStr[first];while (first < last){while (first < last&&pStr[last] >= key)--last;pStr[first] = pStr[last];while (first < last && pStr[first] <= key)++first;pStr[last] = pStr[first];}pStr[first] = key;QuickSort(pStr, low, first - 1);QuickSort(pStr, first + 1, hight);}int main(){char p[] = "acba";Premutation(p);cout << endl;}
0 0
- Java的字符串排序
- 字符串的排序
- 字符串的排序
- 字符串的排序
- 字符串排序的问题
- HWOJ 字符串的排序
- 字符串的排序
- 字符串的冒泡排序
- 字符串的排序
- [python]字符串的排序
- 字符串的排序
- 字符串的排序问题
- 字符串的sort排序
- Q28:字符串的排序
- 【C】字符串的排序
- 巧妙的字符串排序
- 字符串的冒泡排序
- 字符串的冒泡排序
- System.currentTimeMillis()和SystemClock.uptimeMillis()
- 老程序员的忠告:不要做浮躁的软件工程师
- MyEclipse6.5安装SVN插件方法步骤
- OAuth工作原理随想——让你的系统提供的服务更加安全(二)
- UVA 1473 - Dome of Circus(三分+计算几何)
- 字符串的排序
- 谁调用了InvocationHandler 中的invoke方法?
- 最长回文子串
- R语言数据分析系列之一
- geohash地理编码
- static_cast, dynamic_cast, reinterpret_cast, const_cast区别比较
- STL系列(5):vector 删除重复元素
- ADF中使用Sequence
- 检查drawing中草绘线,是否与零件中截面上的线一致