-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path47.全排列-ii.js
52 lines (46 loc) · 891 Bytes
/
47.全排列-ii.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* @lc app=leetcode.cn id=47 lang=javascript
*
* [47] 全排列 II
*/
// @lc code=start
/**
* @param {number[]} nums
* @return {number[][]}
*/
var swap = function(nums, i, j) {
if (i === j)
return;
const t = nums[i];
nums[i] = nums[j];
nums[j] = t;
};
var cal = function (nums, first, result) {
if (nums.length === first) {
result.push([...nums]);
return;
}
const map = new Map();
for (let i = first; i < nums.length; i++) {
if (!map.get(nums[i])) {
map.set(nums[i], true);
swap(nums, first, i);
cal(nums, first + 1, result);
swap(nums, first, i);
}
}
};
/**
* @param {number[]} nums
* @return {number[][]}
*/
var permuteUnique = function(nums) {
if (nums == null)
return;
nums.sort((a, b) => a - b);
const res = [];
cal(nums, 0, res);
return res;
};
s
// @lc code=end