forked from xiaoyu2er/leetcode-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path046-Permutations.js
38 lines (37 loc) · 872 Bytes
/
046-Permutations.js
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
/**
* https://leetcode.com/problems/permutations/description/
* Difficulty:Medium
*
* Given a collection of distinct numbers, return all possible permutations.
* For example,
* [1,2,3] have the following permutations:
* [
* [1,2,3],
* [1,3,2],
* [2,1,3],
* [2,3,1],
* [3,1,2],
* [3,2,1]
* ]
*/
/**
* @param {number[]} nums
* @return {number[][]}
*/
var permute = function (nums) {
if (!nums.length) return [];
var res = [[]];
for (var i = 0; i < nums.length; i++) {
var len = res.length;
for (var j = 0; j < len; j++) {
var oldArr = res.shift();
for (var k = 0; k <= oldArr.length; k++) {
var newArr = oldArr.slice();
newArr.splice(k, 0, nums[i]);
res.push(newArr);
}
}
}
return res;
};
console.log(permute([1, 2, 3]));