练习:全排列

练习:全排列

1、题目要求

输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。

2、代码(使用STL):

class Solution {
public:
    vector<string> Permutation(string str) {
        if(str.empty()) return vector<string>();
        vector<string> res;
        sort(str.begin(), str.end());
        do{
            res.emplace_back(str);
        }while(next_permutation(str.begin(), str.end()));
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/houzijushi/article/details/81124607