题解 | 没有重复项数字的全排列

没有重复项数字的全排列

https://www.nowcoder.com/practice/4bcf3081067a4d028f95acee3ddcd2b1

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param num int整型一维数组 
# @return int整型二维数组
#
class Solution:
    def permute(self , num: List[int]) -> List[List[int]]:
        # 去重、排序
        num_set = set(num)
        sorted_list = sorted(num_set)
        # 最终结果列表
        result_list = []
        # 每个列表
        path = []
        # 是否使用过
        used = [False] * len(num)

        def backtrack():
            
            if len(path) == len(sorted_list):
                result_list.append(path.copy())
                return
            for i in range(len(sorted_list)):
                if used[i]:
                    continue
                path.append(sorted_list[i])
                used[i] = True
                # 递归
                backtrack()

                path.pop()
                used[i] = False
        
        backtrack()
        return result_list
            

        

全部评论

相关推荐

千千倩倩:简历问题有点多,加v细聊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务