forked from sl1673495/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
let uniqSymbol = 'X' | ||
|
||
let permuteUnique = function (nums) { | ||
let n = nums.length | ||
if (n === 1) { | ||
return [nums] | ||
} | ||
let permuteSet = (nums) => { | ||
let n = nums.length | ||
if (n === 0) { | ||
return new Set() | ||
} | ||
if (n === 1) { | ||
return new Set(nums) | ||
} | ||
|
||
let res = new Set() | ||
for (let i = 0; i < n; i++) { | ||
let use = nums[i] | ||
if (use === undefined) { | ||
continue | ||
} | ||
let rest = nums.slice(0, i).concat(nums.slice(i + 1, n)) | ||
let restPermuteds = permuteSet(rest) | ||
restPermuteds.forEach((restPermuted) => { | ||
res.add(`${use}${uniqSymbol}${restPermuted}`) | ||
}) | ||
} | ||
|
||
return res | ||
} | ||
|
||
let permuted = permuteSet(nums) | ||
|
||
return Array.from(permuted).map((val) => val.split(uniqSymbol).map(Number)) | ||
} | ||
|
||
console.log(permuteUnique([-1,2,-1,2,1,-1,2,1])) |