题目描述

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

输入描述:

输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。

class Solution {
public:
    vector<string> Permutation(string str) {

        
        if (str.empty())
            return ans;
        chang(str,0,str.size());
        sort(ans.begin(),ans.end());
        auto it=unique(ans.begin(),ans.end());
        ans.erase(it,ans.end());
        return ans;
        
        
    }
    void chang(string &str,int start ,int len){
        if (start ==len)
            ans.push_back(str);
        for (int i=start;i<len;i++){
            swap(str[start],str[i]);
            chang(str,start+1,len);
            swap(str[start],str[i]);
        }
    }
    vector< string> ans;
};

相关文章:

  • 2022-01-17
  • 2021-05-31
  • 2021-05-25
  • 2021-07-06
  • 2021-11-21
  • 2022-01-08
  • 2021-10-18
猜你喜欢
  • 2022-01-29
  • 2021-11-28
  • 2022-12-23
  • 2022-12-23
  • 2019-11-28
  • 2021-11-13
相关资源
相似解决方案