LeetCode Permutations

来源:互联网 发布:透视眼软件 编辑:程序博客网 时间:2024/06/05 05:13

题目:

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].

class Solution {public:vector<vector<int> > permute(vector<int> &num) {res.clear();memset(used, false, sizeof(used));dfs(0, num.size(), num);return res;}private:vector<vector<int> > res;bool used[100];int a[100];void dfs(int dep, int maxDep, vector<int> &num) {if (dep == maxDep) {vector<int> tt;for (int i = 0; i < maxDep; i++)tt.push_back(a[i]);res.push_back(tt);return;}for (int i = 0; i < maxDep; i++) {if (!used[i]) {used[i] = true;a[dep] = num[i];dfs(dep + 1, maxDep, num);used[i] = false;}}}};


0 0