# LeetCode: Permutations Solution

Backtracking with chosen state

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}var permute = function (nums) {2  const n = nums.length3  const chosen = Array(n).fill(false)4  const res = []5
6  const recursion = (permutation = []) => {7    if (permutation.length === n) {8      res.push([...permutation])9      return10    }11
12    for (let i = 0; i < n; i++) {13      if (chosen[i]) continue14      permutation.push(nums[i])15      chosen[i] = true16      recursion(permutation)17      chosen[i] = false18      permutation.pop()19    }20  }21
22  recursion()23
24  return res25}```

Original problem

## Similar problems

Next Permutation

Permutations II

Permutation Sequence

Combinations

leetcode

array

backtracking

## Next Post

LeetCode: House Robber

Sep 25, 2021

Memoized recusion

## Previous Post

LeetCode: Combinations

Sep 25, 2021

"Don't try" - Charles Bukowski

Search Posts