剑指offer:字符串的排列
这里面用到了一个函数next_permutation()它是一个计算全排列的函数;
首先定义一个数组型字符串的函数,如果字符串的长度为空,则返回原数组,在定义个字符串型的数组result,先把原字符串进行从小到大的排序,用一个do,while循环,当函数next_permutation()进行全排列后,把字符串全压入result的数组里,最后返回这个数组即为所求!!!
#include <algorithm>
class Solution{
public:
vector<string> Permutation(string str){
if(str.size()==0) return vector<string>();
vector<string> result;
sort(str.begin(),str.end());
do{
result.push_back(str);
}while(next_permutation(str.begin(), str.end()));
return result;
}
};
#剑指offer##23届找工作求助阵地#