-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutations.cpp
More file actions
31 lines (28 loc) · 895 Bytes
/
permutations.cpp
File metadata and controls
31 lines (28 loc) · 895 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <vector>
using namespace std;
void permutation(string &arr, int begin, int end, vector<vector<int>>& allPermu){
if(begin == end){
vector<int> temp;
for(int i=0; i<=end; i++){
// cout << arr[i] << " ";
temp.push_back(arr[i]); // leetcode 格式
}
// cout << endl;
allPermu.push_back(temp); // leetcode 格式
}else
{ // ex: a b c d
for(int i=begin; i<=end; i++){
swap(arr[begin], arr[i]); // 對調找排列組合 (ex: b a c d)
permutation(arr, begin+1, end); // 固定前面,把後面繼續置換 (a c d)
swap(arr[begin], arr[i]); // 換回來以便後面繼續對調,換回 a b c d
}
}
}
int main(){
vector<vector<int>> allPermu;
string str = "ABC";
int endNum = str.size()-1;
permutation(str, 0, endNum, allPermu);
return 0;
}