Dev/Algorithm

46. Permutations (Medium)

rryu09 2024. 11. 11. 02:56
class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        res =[]
        s=[]
        v ={i: False for i in nums}

        def dfs():
            if len(s)==len(nums):
                res.append(s[:])
                return
            else:
                for i in nums:
                    if not v[i]:
                        s.append(i)
                        v[i] = True
                        dfs()
                        s.pop()
                        v[i] = False
        dfs()
        return res

백트래킹 오랜만에 연습

'Dev > Algorithm' 카테고리의 다른 글

27. Remove Element (Easy)  (0) 2024.11.12
290. Word Pattern (Easy)  (0) 2024.11.12
36. Valid Sudoku (Medium)  (0) 2024.11.10
226. Invert Binary Tree (Easy)  (0) 2024.11.09
121. Best Time to Buy and Sell Stock (Easy)  (0) 2024.11.08