LSGO——LeetCode实战(数组系列): 46题 全排列 (Permutations)

原题:

给定一个没有重复数字的序列,返回其所有可能的全排列。

示例:

输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/permutations
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这个题,我是真的一点思路都没有,看来社区大神的回溯方法,必须点赞一下,并表示好好学习一下。

解法一:(回溯的思想)

** 这种以深度优先方式系统搜索问题解的算法称为回溯法**
这个程序我是直接复制的leetcode社区的,虽然看懂了,但是对于自己能否自己写出来,还是完全没有把握,所以我想尝试一下运用迭代一下。

import copy
class Solution(object):
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        if len(nums) == 0:
            return []

        used = [False] * len(nums)
        res = []
        self.__dfs(nums, 0, [], used, res)
        return res
  
    def __dfs(self, nums, index, pre, used, res):
        # 先写递归终止条件
        if index == len(nums):
            res.append(copy.copy(pre))
            return

        for i in range(len(nums)):
            if not used[i]:
                # 如果没有用过,就用它
                used[i] = True
                pre.append(nums[i])

                # 在 dfs 前后,代码是对称的
                self.__dfs(nums, index + 1, pre, used, res)

                used[i] = False
                pre.pop()

程序二

利用位掩码来替换上一个程序中的used列表的功能。

class Solution(object):
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        size = len(nums)
        if size == 0:
            return []

        # used = [False] * len(nums)
        state = 0
        res = []
        self.__dfs(nums, 0, size, [], state, res)
        return res

    def __dfs(self, nums, index, size, pre, state, res):
        # 先写递归终止条件
        if index == size:
            res.append(pre[:])
            return

        for i in range(size):
            if ((state >> i) & 1) == 0:
                # 如果没有用过,就用它
                state ^= (1 << i)
                pre.append(nums[i])

                # 在 dfs 前后,代码是对称的
                self.__dfs(nums, index + 1, size, pre, state, res)
                # 状态重置
                state ^= (1 << i)
                pre.pop()

解法二:递归交换

from typing import List


class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        size = len(nums)
        if size == 0:
            return []

        res = []
        self.__helper(nums, 0, size, res)
        return res

    def __helper(self, nums, begin, size, res):
        if begin == size - 1:
            # 打开注释,看看程序是如何运行的
            # print('调试:', nums)
            res.append(nums.copy())
            return
        self.__helper(nums, begin + 1, size, res)
        # 从 begin 的下一位开始一直要交换到最后一位
        for index in range(begin + 1, size):
            nums[begin], nums[index] = nums[index], nums[begin]
            self.__helper(nums, begin + 1, size, res)
            # 注意:递归完成以后要交换回来
            nums[begin], nums[index] = nums[index], nums[begin]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值