Skip to content

Commit ebc7d7a

Browse files
authored
add 44-Permutations
1 parent bde7f31 commit ebc7d7a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

javascript/46-Permutations.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function permute(nums) {
2+
const res = [];
3+
4+
if (nums.length === 1) {
5+
return [nums.slice()];
6+
}
7+
8+
for (const i of nums) {
9+
let n = nums.shift();
10+
11+
let perms = permute(nums);
12+
13+
for (const perm of perms) {
14+
perm.push(n);
15+
}
16+
17+
perms.forEach((perm) => {
18+
res.push(perm);
19+
});
20+
21+
nums.push(n);
22+
}
23+
24+
return res;
25+
}

0 commit comments

Comments
 (0)