46. Permutations Leetcode Python

本文提供了一种高效的方法来生成一组数字的所有可能排列组合,并通过实例演示了实现过程。利用深度优先搜索(DFS)算法,我们可以在O(n!)的时间复杂度下生成所有排列,同时使用额外的O(n)空间来标记每个元素是否已被使用。

Given a collection of numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:
[1,2,3][1,3,2][2,1,3][2,3,1][3,1,2], and [3,2,1].



This problem is exponential. We can use one O(n) extra space to mark whether each element is used or not.

It is DFS.

class Solution:
    # @param num, a list of integer
    # @return a list of lists of integers
    def bfs(self,num,val,res,used):
        if len(val)==len(num) and val not in res:
            res.append(val)
        for i in range(len(num)):
            if used[i]==False:
                used[i]=True
                val=val+[num[i]]
                self.bfs(num,val,res,used)
                val=val[:len(val)-1]
                used[i]=False
    def permute(self, num):
        res=[]
        if len(num)==0:
            return res
        used=[False]*len(num)
        self.bfs(num,[],res,used)
        return res
        


### LeetCode46Python 实现 以下是针对LeetCode46题——PermutationsPython解决方案。该问题的核心在于生成给定列表的所有可能排列组合。 #### 方法一:回溯法 (Backtracking) 通过递归的方式构建每一种排列的可能性,使用`set()`或者布尔数组来标记已经使用的元素,从而避免重复计算。 ```python class Solution: def permute(self, nums: list[int]) -> list[list[int]]: result = [] def backtrack(path, remaining): if not remaining: # 如果剩余元素为空,则当前路径即为一个完整的排列 result.append(path[:]) return for i in range(len(remaining)): current_num = remaining[i] next_remaining = remaining[:i] + remaining[i+1:] path.append(current_num) # 将当前数字加入路径 backtrack(path, next_remaining) # 继续处理剩下的数字 path.pop() # 回溯,移除最后一个数字 backtrack([], nums) return result ``` 此方法的时间复杂度为O(N!),其中N为输入列表长度[^1]。 --- #### 方法二:插入法 (Insertion Method) 另一种思路是从空列表开始逐步扩展,每次将新数字插入已有排列的不同位置形成新的排列集合。 ```python class Solution: def permute(self, nums: list[int]) -> list[list[int]]: permutations = [[]] for num in nums: new_permutations = [] for permutation in permutations: for i in range(len(permutation) + 1): new_permutation = permutation[:i] + [num] + permutation[i:] new_permutations.append(new_permutation) permutations = new_permutations return permutations ``` 这种方法同样具有时间复杂度O(N!),但在某些场景下更直观易懂[^2]。 --- #### 方法三:DFS (Depth First Search) 利用深度优先搜索的思想,在每一层递归中选取尚未被固定的元素作为当前位置候选者,并继续向下探索直至完成整个序列的选择过程。 ```python class Solution: def permute(self, nums: list[int]) -> list[list[int]]: def dfs(ans, nums, path): if len(nums) == 0: ans.append(path) return for i in range(len(nums)): dfs(ans, nums[:i] + nums[i+1:], path + [nums[i]]) answer = [] dfs(answer, nums, []) return answer ``` 这种方式简洁明了,易于理解和实现[^3]。 --- ### 总结 以上三种算法均能有效解决LeetCode46题的要求,具体选择取决于个人偏好以及实际应用场景下的性能考量因素。无论是采用显式的状态维护还是隐含的状态传递机制,这些方案都能够满足题目对于不重复全排列生成的需求。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值