#46
Permutations
medium· Backtrackingruns: 0Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 577
class Solution:
def permute(self, nums: list[int]) -> list[list[int]]:
result = []
path = []
used = [False] * len(nums)
def backtrack() -> None:
if len(path) == len(nums):
result.append(path[:])
return
for i in range(len(nums)):
if used[i]:
continue
used[i] = True
path.append(nums[i])
backtrack()
path.pop()
used[i] = False
backtrack()
return result
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.