Skip to content

Commit cead5ef

Browse files
authored
Update 47-permutations-ii.js
1 parent 8bd1a0a commit cead5ef

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

47-permutations-ii.js

+33
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,36 @@ function permuteUniqueHelper(m, l, p, i, r) {
2929
}
3030
}
3131
}
32+
33+
34+
// another
35+
36+
/**
37+
* @param {number[]} nums
38+
* @return {number[][]}
39+
*/
40+
const permuteUnique = function(nums) {
41+
const set = new Set()
42+
const used = new Set()
43+
bt(nums, 0, [], used, set)
44+
const res = []
45+
for(let item of set) {
46+
res.push(item.split(','))
47+
}
48+
return res
49+
};
50+
51+
function bt(nums, i, cur, used, set) {
52+
if(i === nums.length) {
53+
set.add(cur.slice().join(','))
54+
return
55+
}
56+
for(let idx = 0; idx < nums.length; idx++) {
57+
if(used.has(idx)) continue
58+
cur.push(nums[idx])
59+
used.add(idx)
60+
bt(nums, i + 1, cur, used, set)
61+
used.delete(idx)
62+
cur.pop()
63+
}
64+
}

0 commit comments

Comments
 (0)