Skip to content

Instantly share code, notes, and snippets.

@walkingtospace
Created June 12, 2014 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walkingtospace/afc93a807a945ffbd39b to your computer and use it in GitHub Desktop.
Save walkingtospace/afc93a807a945ffbd39b to your computer and use it in GitHub Desktop.
permutation swap으로 세기
void permutation(vector<vector<int>>& res, vector<int> input, int startIndex) {
if(input.size() <= startIndex) {
res.push_back(input);
} else {
for(int i=startIndex ; i<input.size() ; i++) {
vector<int>temp = input;
int tempElement = temp[i]; //swap
temp[i] = temp[startIndex];
temp[startIndex] = tempElement;
permutation(res, temp, startIndex+1);
}
}
}
int main()
{
vector<vector<int>> res;
vector<int> input = {1,2,3,4};
permutation(res, input, 0);
for(int i=0; i<res.size(); i++) {
for(int j=0; j<res[i].size(); j++) {
cout<<res[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment