8000 Added another solution to Permutation || · SivaCse/leetcode-javascript@efdff0e · GitHub
[go: up one dir, main page]

Skip to content

Commit efdff0e

Browse files
Added another solution to Permutation ||
Similar approach that permutation.js
1 parent 3121d3d commit efdff0e

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

47 Permutations II.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,32 @@ var generatePermute = function(nums, step, currentResult, visited, finalResult)
5050
visited[i] = false;
5151
}
5252
}
53-
}
53+
}
54+
55+
//Another Solution, similar approach that Permutation.js
56+
var permuteUnique = function(nums) {
57+
return permut(nums.sort(), []);
58+
};
59+
60+
var permut = function(nums, partial) {
61+
if(nums.length === 0) {
62+
return [partial];
63+
}
64+
var listSol = [];
65+
for(var i = 0; i < nums.length; i++) {
66+
var endRepeated = i;
67+
while(endRepeated < nums.length && nums[i] === nums[endRepeated]) {
68+
endRepeated++;
69+
}
70+
71+
var arrayWithoutI = nums.slice(0,i).concat(nums.slice(i + 1, nums.length));
72+
var partialSol = partial.concat([nums[i]]);
73+
var sol = permut(arrayWithoutI, partialSol);
74+
if(sol.length > 0){
75+
listSol = listSol.concat(sol);
76+
}
77+
i = endRepeated - 1;
78+
}
79+
return listSol;
80+
};
81+

0 commit comments

Comments
 (0)
0