【剑指offer】字符串的排列

 题目描述

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

输入描述:

输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
class Solution {
public:
    void _Permutation(string str, vector<string>& vRet,int index)
    {
        if(index == str.size()-1)
            vRet.push_back(str);
        for(int i = index; i < str.size();++i)
        {
            if(i != index && str[i] == str[index])
                continue;
            swap(str[i], str[index]);
            _Permutation(str, vRet, index+1);
        }
    }
    vector<string> Permutation(string str) {
        vector<string> vRet;
        if(str.empty()) return vRet;
        int index = 0;
        _Permutation(str,vRet,index);
        sort(str.begin(),str.end());
        return vRet;
    }
};

猜你喜欢

转载自blog.csdn.net/yulong__li/article/details/85054498