面试题28 字符串排列

题目描述

输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。 结果请按字母顺序输出。 
输入描述:
输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
 1 class Solution {
 2 public:
 3     vector<string> v;
 4     
 5     void swap(char *a, char *b){
 6         char temp = *a;
 7         *a = *b;
 8         *b = temp;
 9     }
10     
11     void AllRange(int k, int n, string str){
12         if (k >= n)
13             v.push_back(str);
14         else{
15             for (int i = k; i < n; i++){
16                 swap(&str[k], &str[i]);
17                 AllRange(k + 1, n, str);
18                 swap(&str[k], &str[i]);
19             }
20         }
21     }
22     
23     vector<string> Permutation(string str) {
24         if (str.length() != 0){
25             AllRange(0, str.length(), str);
26             sort(v.begin(),v.end());
27             vector<string>::iterator iter = unique(v.begin(),v.end());
28             if(iter != v.end())
29                 v.erase(iter,v.end());
30         }
31         return v;
32     }
33 };
原文地址:https://www.cnblogs.com/wanderingzj/p/5357429.html