1function permute(nums) {
2 const res = [], path = [], used = Array(nums.length).fill(false);
3 function backtrack() {
4 if (path.length === nums.length) { res.push([...path]); return; }
5 for (let i = 0; i < nums.length; i++) {
6 if (used[i]) continue;
7 used[i] = true; path.push(nums[i]);
8 backtrack();
9 path.pop(); used[i] = false;
10 }
11 }
12 backtrack();
13 return res;
14}