-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy path全排列II.js
More file actions
29 lines (29 loc) · 728 Bytes
/
全排列II.js
File metadata and controls
29 lines (29 loc) · 728 Bytes
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[][]}
47.全排列 II #回溯
*/
var permuteUnique = function(nums) {
const dfs = (curr, store, arr, len) => {
if (curr.length === len) {
arr.push([...curr]);
return;
}
for (let i = 0; i < store.length; i++) {
if (i > 0 && store[i] === store[i - 1]) {
continue
}
let select = store[i]
dfs(
curr.concat(select),
store.slice(0, i).concat(store.slice(i + 1)),
arr,
len
)
}
}
let ans = []
nums.sort((a, b) => a - b);
dfs([], nums, ans, nums.length)
return ans
};