diff --git a/leet-code/46-permutations/README.md b/leet-code/46-permutations/README.md new file mode 100644 index 0000000..cc9cac6 --- /dev/null +++ b/leet-code/46-permutations/README.md @@ -0,0 +1,36 @@ +# 46-permutations + +## Problem Statement + +Given an array `nums` of **distinct integers**, return **all possible permutations** of the array. +You can return the answer in **any order**. + +### Example 1: + +**Input:** +`nums = [1, 2, 3]` + +**Output:** +`[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]` + +### Example 2: + +**Input:** +`nums = [0, 1]` + +**Output:** +`[[0,1],[1,0]]` + +### Example 3: + +**Input:** +`nums = [1]` + +**Output:** +`[[1]]` + +### Constraints: + +- `1 <= nums.length <= 6` +- `-10 <= nums[i] <= 10` +- All the integers of `nums` are **unique** diff --git a/leet-code/46-permutations/index.js b/leet-code/46-permutations/index.js new file mode 100644 index 0000000..36093a5 --- /dev/null +++ b/leet-code/46-permutations/index.js @@ -0,0 +1,22 @@ +/** + * @param {number[]} nums + * @return {number[][]} + */ +var permute = function(nums) { + const backtrack = (start) => { + if (start === nums.length) { + res.push([...nums]); + return; + } + + for (let i = start; i < nums.length; i++) { + [nums[start], nums[i]] = [nums[i], nums[start]]; + backtrack(start + 1); + [nums[start], nums[i]] = [nums[i], nums[start]]; + } + }; + + const res = []; + backtrack(0); + return res; +}; \ No newline at end of file