Error in user YAML: (<unknown>): mapping values are not allowed in this context at line 2 column 6
---
Time:2019/4/30
Title: Permutations
Difficulty: Medium
Author: 小鹿
---
Given a collection of distinct integers, return all possible permutations.
给定一个没有重复数字的序列,返回其所有可能的全排列。
Example:
Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
var permute = function(nums) {
const nl = nums.length;
if(nl === 0) {return [];}
if(nl === 1) {return [nums];}
const result = [];
for(let i = 0; i < nl; i ++) {
const subArr = [];
for(let j = 0; j < nl; j ++) {
if(i !== j) {
subArr.push(nums[j]);
}
}
const subRes = permute(subArr);
const sl = subRes.length;
for(let j = 0; j < sl; j ++){
result.push([nums[i]].concat(subRes[j]));
}
}
return result;
};
// 测试
let arr = [1,2,3];
console.log(permute(arr))