Skip to content

Latest commit

 

History

History
22 lines (21 loc) · 582 Bytes

46. Permutations.md

File metadata and controls

22 lines (21 loc) · 582 Bytes

46. Permutations (medium)

[tag] DFS, permutation

class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        num_set = set()
        temp = []
        ans = []
        def dfs():
            if len(temp) == len(nums):
                ans.append(temp.copy())
            for i in range(0, len(nums)):
                if i not in num_set:
                    temp.append(nums[i])
                    num_set.add(i)
                    dfs()
                    num_set.remove(i)
                    temp.pop()
        dfs()
        return ans