113 lines
2.5 KiB
C++
Raw Normal View History

2014-10-20 11:23:39 +08:00
// Source : https://oj.leetcode.com/problems/permutations/
// Author : Hao Chen
// Date : 2014-06-21
/**********************************************************************************
*
* Given a collection of numbers, return all possible permutations.
*
* For example,
* [1,2,3] have the following permutations:
* [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
*
*
**********************************************************************************/
2014-10-20 11:23:39 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
using namespace std;
/*
{ 1 2 3 }
{ 2 1 3 }
{ 3 2 1 }
{ 1 3 2 }
{ 2 3 1 }
{ 3 1 2 }
*/
2014-10-27 22:43:09 +08:00
/*
* The algroithm - Take each element in array to the first place.
*
* For example:
*
* 0) initalization
*
* pos = 0
* [1, 2, 3]
*
* 1) take each element into the first place,
*
* pos = 1
* [1, 2, 3] ==> [2, 1, 3] , [3, 1, 2]
*
* then we have total 3 answers
* [1, 2, 3], [2, 1, 3] , [3, 1, 2]
*
* 2) take each element into the "first place" -- pos
*
* pos = 2
* [1, 2, 3] ==> [1, 3, 2]
* [2, 1, 3] ==> [2, 3, 1]
* [3, 1, 2] ==> [3, 2, 1]
*
* then we have total 6 answers
* [1, 2, 3], [2, 1, 3] , [3, 1, 2], [1, 3, 2], [2, 3, 1], [3, 2, 1]
*
* 3) pos = 3 which greater than length of array, return.
*
*/
2014-10-20 11:23:39 +08:00
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int> > vv;
vv.push_back(num);
if (num.size() <2){
return vv;
}
int pos=0;
while(pos<num.size()-1){
int size = vv.size();
for(int i=0; i<size; i++){
2014-10-27 22:43:09 +08:00
//take each number to the first place
2014-10-20 11:23:39 +08:00
for (int j=pos+1; j<vv[i].size(); j++) {
vector<int> v = vv[i];
int t = v[j];
v[j] = v[pos];
v[pos] = t;
vv.push_back(v);
}
}
pos++;
}
return vv;
}
int main(int argc, char** argv)
{
int n = 3;
if (argc>1){
n = atoi(argv[1]);
}
vector<int> v;
for (int i=0; i<n; i++) {
v.push_back(i+1);
}
vector<vector<int> > vv;
vv = permute(v);
for(int i=0; i<vv.size(); i++) {
cout << "{ ";
for(int j=0; j<vv[i].size(); j++){
cout << vv[i][j] << " ";
}
cout << "}" <<endl;
}
return 0;
}