46.全排列-py

46.全排列

class Solution(object):
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        # 结果数组0
        ans=[]
        n=len(nums)
        # 判断是否使用
        state_=[False]*n
        # 临时状态数组
        dp_=[]
        

        def dfs (index):
            # 终止条件
            if index==n:
                ans.append(dp_[:])
                return 
            for i in range (n):
                if not state_[i]:
                    state_[i]=True
                    dp_.append(nums[i])
                    dfs(index+1)
                    dp_.pop()
                    state_[i]=False
        dfs(0)
        return ans

你可能感兴趣的:(深度优先,算法)