Skip to content
Merged

solve #116

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions leet-code/46-permutations/README.md
Original file line number Diff line number Diff line change
@@ -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**
22 changes: 22 additions & 0 deletions leet-code/46-permutations/index.js
Original file line number Diff line number Diff line change
@@ -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;
};