剑指offer38字符串的排列

/*可重排列模版题目*/
class Solution {
public:
    
    void print_permutation(vector<string>& ans ,int n , string &A , string s ,int cur){
          if(cur == n){
               ans.push_back(A);
          }
          else for(int i = 0;i < n;i++){
            if(!i || s[i]!= s[i-1]){
             int c1 = 0 , c2 = 0;
             for(int j = 0;j < cur;j++)    if(A[j] == s[i]) c1++;
             for(int j = 0;j < n;j++)     if(s[i] == s[j]) c2++;
             if(c1 < c2){
                  A[cur] = s[i];
                  print_permutation(ans , n , A , s , cur + 1);
             }
            }
          }
    }
    vector<string> permutation(string s) {
         vector<string> ans;
         string A = s;
         sort(s.begin(),s.end());
         print_permutation(ans ,s.size() , A , s , 0);
         return ans;
    }
};
原文地址:https://www.cnblogs.com/rhythmic/p/13964404.html