本文共 2665 字,大约阅读时间需要 8 分钟。
dd
#includeusing namespace std;void swap(char *a, char *b) { char tmp; tmp = *a; *a = *b; *b = tmp; } void perm(char list[], int beg, int end) { if(beg > end) { for(int i = 0; i <= end; i++) cout << list[i]; cout << endl; } else { for(int i = beg; i <= end; i++) { swap(&list[i], &list[beg]); perm(list, beg + 1, end); swap(&list[i], &list[beg]); } } } int main() { char list[] = { '1', '2', '3'}; int lens = sizeof(list) / sizeof(*list); perm(list, 0, lens-1); return 0; }
dd
#include#include using namespace std;void getPer(vector &num, int index, vector &tmp, vector > &rev){ if (index == num.size()) rev.push_back(tmp); for (int i = index; i < num.size(); ++i) { swap(num[i], num[index]); tmp.push_back(num[index]); getPer(num, index+1, tmp, rev); tmp.pop_back(); swap(num[i], num[index]); }}vector > permute(vector &nums){ vector > rev; vector tmp; getPer(nums, 0, tmp, rev); return rev;}int main(){ vector vec; vec.push_back(1); vec.push_back(2); vec.push_back(3); vector > rev = permute(vec); cout << rev.size() << endl;}
dd
class Solution {public: void getPermute(vector &num, int index, vector &tmp, vector> &result) { if (index == num.size()) result.push_back(num); for (int i = index; i < num.size(); ++i) { swap(num[i], num[index]); tmp.push_back(num[index]); getPermute(num, index+1, tmp, result); tmp.pop_back(); swap(num[i], num[index]); } } vector > permute(vector &num) { vector > result; vector tmp; getPermute(num, 0, tmp, result); return result; }};
本文转自jihite博客园博客,原文链接:http://www.cnblogs.com/kaituorensheng/p/3963534.html,如需转载请自行联系原作者