@@ -143,92 +143,30 @@ public:
1431433. 接着,固定第一个字符,求后面所有字符的排列。这个时候我们仍把后面的所有字符分成两部分:后面字符的第一个字符,以及这个字符之后的所有字符。然后把第一个字符逐一和它后面的字符交换
144144
145145```cpp
146- #include <iostream>
147- #include <algorithm>
148- #include <vector>
149- #include <string>
150-
151- using namespace std;
152-
153- // 调试开关
154- #define __tmain main
155-
156- #ifdef __tmain
157-
158- #define debug cout
159-
160- #else
161-
162- #define debug 0 && cout
163-
164- #endif // __tmain
165-
166-
167- class Solution
168- {
169- protected:
170- vector<string> m_res;
171-
172- public:
173-
174- vector<string> Permutation(string str)
175- {
176- m_res.clear( );
177-
178- if(str.empty( ) == true)
179- {
180- return m_res;
181- }
182- PermutationRecursion(str, 0);
183-
184- sort(m_res.begin( ), m_res.end( ));
185- return m_res;
146+ vector<string> Permutation(string str) {
147+ vector<string> v1;
148+ if(str.size()<= 0 || str.size()>9 )//排除异常情况
149+ return v1;
150+ PermutationHelp(v1, 0, str);
151+ return v1;
186152 }
187-
188-
189- void PermutationRecursion(string str, int begin)
190- {
191- if(str[begin] == '\0')
192- {
193- debug <<str <<endl;
194- m_res.push_back(str);
195- }
196- else
197- {
198- for(int i = begin;
199- str[i] != '\0';
200- i++)
201- {
202- //debug <<str[i] <<str[begin] <<endl;
203- if(!HasDuplicate(str, begin, i))
204- {
205- swap(str[i], str[begin]);
206- debug <<"swap " <<str[i] <<"(" <<i <<")" <<" and " <<str[begin] <<"(" <<begin <<")" <<endl;
207- PermutationRecursion(str, begin + 1);
208- //copy(str.begin( ), str.degin( ) + i, ostream_iterator<char>(cout," "));
209- swap(str[i], str[begin]);
210- }
211- }
212- }
213- }
214-
215- private:
216- //find duplicate of str[i] in str[k,i)
217- bool HasDuplicate(string& str, int k, int i) const {
218- for (int p = k; p < i; p++)
219- if (str[p] == str[i]) return true;
220-
221- return false;
222- }
223- };
224-
225- int __tmain( )
226- {
227- Solution solu;
228- solu.Permutation("abc");
229-
230- return 0;
231- }
153+ void PermutationHelp(vector<string> &ans, int k, string str) //遍历第k位的所有可能
154+ {
155+ if(k == (str.size() - 1))//单次递归终止条件
156+ ans.push_back(str);
157+ unordered_set<char> us;//保存字符串中字符的无序集合
158+ sort(str.begin() + k, str.end());
159+ for(int i = k; i < str.size(); i++)
160+ {
161+ if(us.find(str[i]) == us.end())
162+ {
163+ us.insert(str[i]);
164+ swap(str[i], str[k]);
165+ PermutationHelp(ans, k + 1, str);
166+ swap(str[i], str[k]);
167+ }
168+ }
169+ }
232170```
233171
234172#STL的next_permutation求全排列
@@ -263,4 +201,51 @@ public:
263201 }
264202};
265203```
204+ # 字符串的全组合
205+ ``` cpp
206+ #include < iostream>
207+ #include < vector>
208+ #include < cstring>
209+ #include < assert.h>
210+ using namespace std ;
211+
212+ void Combination (char * string ,int number,vector<char > &result);
213+
214+ void Combination(char * string)
215+ {
216+ assert(string != NULL);
217+ vector<char > result;
218+ int i , length = strlen(string);
219+ for(i = 1 ; i <= length ; ++i)
220+ Combination(string , i ,result);
221+ }
266222
223+ void Combination(char * string ,int number , vector<char > &result)
224+ {
225+ assert(string != NULL);
226+ if(number == 0)
227+ {
228+ static int num = 1;
229+ printf("第%d个组合\t",num++);
230+ vector<char >::iterator iter = result.begin();
231+ for( ; iter != result.end() ; ++iter)
232+ printf("%c",* iter);
233+ printf("\n");
234+ return ;
235+ }
236+ if(* string == '\0')
237+ return ;
238+ result.push_back(* string);
239+ Combination(string + 1 , number - 1 , result);
240+ result.pop_back();
241+ Combination(string + 1 , number , result);
242+ }
243+
244+ int main(void)
245+ {
246+ char str[ ] = "abc";
247+ Combination(str);
248+ return 0;
249+ }
250+
251+ ```
0 commit comments