Skip to content

Commit 53cdf84

Browse files
author
zhangzhengxian
committed
7.11 commit
1 parent 533bfcc commit 53cdf84

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

algorithms/Permutations.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[][]}
4+
*/
5+
var permute = function(nums) {
6+
var result = [],
7+
len = nums.length;
8+
var addNum = function(used, arr) {
9+
var temp = arr.slice(0);
10+
for (var i = 0; i < len; i++) {
11+
if (used[i] === true) {
12+
continue;
13+
}
14+
temp.push(nums[i]);
15+
if (temp.length === len) {
16+
result.push(temp);
17+
return
18+
}
19+
used[i] = true;
20+
addNum(used, temp);
21+
used[i] = false;
22+
temp.pop();
23+
}
24+
}
25+
addNum([], []);
26+
return result;
27+
};

0 commit comments

Comments
 (0)