-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutationII.js
More file actions
43 lines (36 loc) · 1.05 KB
/
Copy pathpermutationII.js
File metadata and controls
43 lines (36 loc) · 1.05 KB
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
/**
* @param {number[]} nums
* @return {number[][]}
*/
function rearrange(arr, firstIndex, secondIndex, result ) {
if(firstIndex == arr.length-1){
result.push([...arr]);
return;
}
let hashMap = {};
let temp = arr[firstIndex];
arr[firstIndex] = arr[secondIndex];
arr[secondIndex] = temp;
for(let itr = firstIndex+1 ; itr < arr.length ; itr++) {
if(!hashMap.hasOwnProperty(arr[itr])) {
rearrange(arr, firstIndex + 1, itr, result);
hashMap[arr[itr]] = 1;
}
}
temp = arr[firstIndex];
arr[firstIndex] = arr[secondIndex];
arr[secondIndex] = temp;
}
var permuteUnique = function(nums) {
let result = [];
//nums.sort((a,b)=>{return a-b;})
let hashMap = {};
for(let itr = 0 ; itr < nums.length ; itr++) {
if(!hashMap.hasOwnProperty(nums[itr])) {
rearrange(nums, 0, itr, result);
hashMap[nums[itr]] = 1;
}
}
return result;
};
//https://leetcode.com/problems/permutations-ii/