1. 程式人生 > >【劍指offer】字串的排列

【劍指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;
    }
};