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

没有重复项数字的全排列

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

回溯的思想,当遍历到响应的元素后,注意还有一步测回的操作。

class Solution:

    def permute(self , num: List[int]) -> List[List[int]]:
        # write code here
        if not num:
            return None
        num.sort()
        res = []
        self.backtrack(num, res, 0)
        return res
        
        
    def backtrack(self, num, res, index):
        if index == len(num) - 1:
            res.append(num)
        else:
            for i in range(index, len(num)):
                temp = num[i]
                num[i] = num[index]
                num[index] = temp 
                self.backtrack(num, res, index + 1)
                temp = num[i]
                num[i] = num[index]
                num[index] = temp
        
        
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-09 11:30
找工作7个月,投了7000封,3段世界五百强实习,才有一个offer,牛油们肯定比我强吧
码农索隆:不对不对不对,实习经历这么厉害,简历也没少投,问题出在哪呢
点赞 评论 收藏
分享
头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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