Q9.5 Write a method to compute all permutations of a string
2015-01-25 15:29
561 查看
Q:Write a method to compute all permutations of a string
A:为了处理方便,下面的代码不是求一个string的全排列,而是求数组的全排列,本质上是一致的
1、如果数据不存在重复。例如[1,2,3]
想要得到全排列,就是要逐位交换。即1与自身交换,然后加上[2,3]的全排列,这只是以1开始的序列;接下来1与2交换,得[2,1,3],如法炮制,2加上[1,3]的全排列;最后1与3交换,得到[3,1,2], 3加上[1,2]的全排列,就得到以3开始的排列。其中子数组的全排列也用同样的方法就可以得到。
2、如果数组中存在重复元素,那么再交换数据之前就需要进行一下判断。
重复元素之后的元素将不在进行交换,例如[1,1,2,3,5,6]
index = 0时候的1,不与后面的元素交换。而是用index = 1时候的1与后面的元素交换,这样去除结果中重复的元素
A:为了处理方便,下面的代码不是求一个string的全排列,而是求数组的全排列,本质上是一致的
1、如果数据不存在重复。例如[1,2,3]
想要得到全排列,就是要逐位交换。即1与自身交换,然后加上[2,3]的全排列,这只是以1开始的序列;接下来1与2交换,得[2,1,3],如法炮制,2加上[1,3]的全排列;最后1与3交换,得到[3,1,2], 3加上[1,2]的全排列,就得到以3开始的排列。其中子数组的全排列也用同样的方法就可以得到。
2、如果数组中存在重复元素,那么再交换数据之前就需要进行一下判断。
重复元素之后的元素将不在进行交换,例如[1,1,2,3,5,6]
index = 0时候的1,不与后面的元素交换。而是用index = 1时候的1与后面的元素交换,这样去除结果中重复的元素
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; //没有重复元素 void dfs1(vector<int> num, int start, vector<vector<int> > &res) { if (start == num.size()) { res.push_back(num); return ; } for (int i = start; i < num.size(); i++) { swap(num[i],num[start]); dfs1(num, start+1, res); swap(num[i],num[start]); } } vector<vector<int> > permute(vector<int> &num) { vector<vector<int> > res; res.clear(); dfs1(num,0, res); return res; } void printResult(vector<vector<int> > &res) { for (int i = 0; i < res.size(); i++) { cout<<"["; for (int j = 0; j <res[i].size(); j++) { cout<<res[i][j]; if (j != res[i].size()-1) { cout<<","; } } cout<<"]"<<endl; } } //存在重复元素的情况 bool isSwap(vector<int> &num, int s, int e) { int i = s; while (i < e && num[i] != num[e]) { i++; } if (i == e) return true; else return false; } void dfs2(vector<int> num, int start, vector<vector<int> > &res) { if (start == num.size()) { res.push_back(num); return ; } for (int i = start; i < num.size(); i++) { if (!isSwap(num, start, i)) continue; swap(num[i],num[start]); dfs2(num, start+1, res); swap(num[i],num[start]); } } vector<vector<int> > permuteDup(vector<int> &num) { vector<vector<int> > res; res.clear(); dfs2(num,0, res); return res; } int main() { vector<int> A; for (int i = 1; i <= 3; i++) { A.push_back(i); } cout<<"输入:["<<A[0]; for (int i = 1; i < A.size(); i++) { cout<<","<<A[i]; } cout<<"]"<<endl; vector<vector<int> > res = permute(A); printResult(res); cout<<endl; A[1] = 1; A[2] = 2; cout<<"输入:["<<A[0]; for (int i = 1; i < A.size(); i++) { cout<<","<<A[i]; } cout<<"]"<<endl; res = permuteDup(A); printResult(res); return 0; }
相关文章推荐
- Write a method to compute all permutations of a string
- Write a program to print all permutations of a given string
- Write a C program to print all permutations of a given string
- Ch1-5: Write a method to replace all spaces in a string with ‘%20’.
- Write a method to replace all spaces in a string with ‘%20’.
- Write a method to replace all spaces in a string with ‘%20’
- 【待】1.4 Write a method to replace all spaces in a string with'%20'.
- Q1.4 Write a method to replace all spaces in a string with ‘%20’.
- [CC150] Get all permutations of a string
- Write an Efficient Method to Check if a Number is Multiple of 3(快速判断3的倍数)
- The comparison of the toString() method and the valueOf(...) method of the String class
- Algorithm_Write a method that returns all subsets of a set.
- All-In-One Code Framework(AIO): 如何使用C#编写进程外的COM组件 (How to write an out-of-proc COM server in C#)
- 1.3 Given two strings, write a method to decide if one is a permutation of the other.
- Write a method to print the last K lines of an input file using C++
- BigDecimal(-)Difference of BigDecimal toString() method between JDK1.4 and above version
- Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthe
- Q9.4 Write a method that returns all subsets of a set.
- Write an Efficient Method to Check if a Number is Multiple of 3
- Write a method that returns all subsets of a set.