Search

[46] Permutations

태그
Back Tracking
Tier
Medium
날짜
2025/05/13

Question

Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

My Answer

class Solution: def permute(self, nums: List[int]) -> List[List[int]]: res = [] def backtrack(i): if i == len(nums): res.append(nums[:]) return for j in range(i, len(nums)): nums[i], nums[j] = nums[j], nums[i] backtrack(i + 1) nums[i], nums[j] = nums[j], nums[i] backtrack(0) return res
Python
복사