给定一个无重复数字序列,返回其所有可能组合
123 132 213 231 312 321
let permute = function (nums) {
let len = nums.length;
let result = [];
let used = [];
let dfs = function (path) {
if (path.length === len) {
return result.push(path.slice());
}
for (let i = 0; i < len; i++) {
if (used[nums[i]]) continue;
used[nums[i]] = true;
path.push(nums[i]);
dfs(path);
path.pop();
used[nums[i]] = false;
}
}
dfs([]);
return result;
};
console.log(permute([1,3]))