forked from lazzzis/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
29 lines (28 loc) · 772 Bytes
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* @param {number[]} nums
* @return {number[][]}
*/
var permuteUnique = function (nums) {
function dfs (curPer, choices) {
// console.log(curPer, choices, ans)
if (curPer.length === nums.length) {
ans.push(curPer)
return
}
const choicesArr = Object.keys(choices)
for (const next of choicesArr) {
choices[next]--
if (choices[next] === 0) Reflect.deleteProperty(choices, next)
dfs(curPer.concat([ parseInt(next, 10) ]), choices)
choices[next] = choices[next] == null ? 1 : choices[next] + 1
}
}
const ans = []
const choices = {}
nums.forEach((item) => {
choices[item] = choices[item] == null ? 1 : choices[item] + 1
})
dfs([], choices)
return ans
}
console.log(permuteUnique([1, 2, 2]))