Skip to content

Commit 9ef8249

Browse files
committed
permute
1 parent 38da47f commit 9ef8249

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

permute.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[][]}
4+
*/
5+
var permute = function(nums) {
6+
let results = []
7+
8+
function permute(current = [], cache = {}){
9+
if (current.length === nums.length){
10+
results.push(current.slice())
11+
return
12+
}
13+
for (const num of nums){
14+
if (!cache[num]){
15+
cache[num] = true
16+
current.push(num)
17+
permute(current, cache)
18+
current.pop()
19+
cache[num] = false
20+
}
21+
}
22+
}
23+
24+
permute()
25+
return results
26+
};

0 commit comments

Comments
 (0)