-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
74 lines (72 loc) · 1.92 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* @lc app=leetcode id=47 lang=javascript
*
* [47] Permutations II
*
* https://leetcode.com/problems/permutations-ii/description/
*
* algorithms
* Medium (38.84%)
* Total Accepted: 224.4K
* Total Submissions: 570.5K
* Testcase Example: '[1,1,2]'
*
* Given a collection of numbers that might contain duplicates, return all
* possible unique permutations.
*
* Example:
*
*
* Input: [1,1,2]
* Output:
* [
* [1,1,2],
* [1,2,1],
* [2,1,1]
* ]
*
*
*/
/**
* 思路:
*
* 1. 我们用一个字典记录我们使用过的数字,主要记录nuns的index
* 2. 对于第一位我们可以选择任意一位nums来作为第一位
* 3. 当数组长度和nums一样的时候,说明所有的nums都做了选择
* 4. 因为数组的数字可能有重复的,我们需要对重复进行排重
* 5. 因为重复的数字谁先选第一位都是一样的,所以我们就让重复的数字的第一位选择第一位
* 6. 如果当前数字和前一位数字相等则说明这个情况已经出现过了,可以跳过
*
*
* @param {number[]} nums
* @return {number[][]}
*/
var permuteUnique = function(nums) {
const result = [];
nums.sort((a, b) => a - b);
permuteUniqueHelp([], {});
return result;
function permuteUniqueHelp(tmp, used) {
if (tmp.length === nums.length) {
result.push(tmp);
return;
}
for (let i = 0; i < nums.length; i++) {
if (used[i] || i > 0 && !used[i - 1] && nums[i - 1] === nums[i]) {
continue;
}
used[i] = true;
tmp.push(nums[i]);
permuteUniqueHelp([...tmp], {...used});
used[i] = false;
tmp.pop();
}
}
};
console.log(permuteUnique([1,1,2]));
module.exports = {
id:'47',
title:'Permutations II',
url:'https://leetcode.com/problems/permutations-ii/description/',
difficulty:'Medium',
}