Created
June 12, 2014 17:00
-
-
Save walkingtospace/afc93a807a945ffbd39b to your computer and use it in GitHub Desktop.
permutation swap으로 세기
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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