本文共 635 字,大约阅读时间需要 2 分钟。
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
输入描述: 输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。class Solution {public: void per(vector&res,string str,unsigned long index) //注意传入的参数要用引用 { if(index==str.size()-1) //递归结束的条件就是,第一位和最后一位交换完成 { res.push_back(str); return; } for(unsigned long i=index;i Permutation(string str) { vector res; if(str.size()==0) return res; //判断输入的是空 per(res,str,0); sort(res.begin(),res.end()); //要求排序,所以全部打印出来后,用sort排一下序 return res; } };
转载地址:http://eixgj.baihongyu.com/