leetcode 46 Permutations Python 实现(回溯算法)

Given a collection of distinct integers, return all possible permutations.

Example:

Input: [1,2,3]Output:[  [1,2,3],  [1,3,2],  [2,1,3],  [2,3,1],  [3,1,2],  [3,2,1]]

回溯算法自己的一个思路如下:需要保留数字使用情况,my_nums增加和删除元素等操作
 1 class Solution:
 2     def permute(self, nums: List[int]) -> List[List[int]]:
 3         results = []
 4         use_dict = dict.fromkeys(nums, False) #初始化一个字典,保存数字是否使用过
 5         def backtrack(results, my_nums, use_dict):
 6             if len(my_nums) == len(nums):
 7                 tmp_nums = copy.deepcopy(my_nums)
 8                 results.append(tmp_nums)
 9                 return
10
11             for x in nums:
12                 if not use_dict[x]:
13                     use_dict[x] = True
14                     my_nums.append(x)
15                     backtrack(results, my_nums, use_dict)
16                     my_nums.remove(x)  #remove操作耗费时间
17                     use_dict[x] = False
18         backtrack(results, [], use_dict)
19         return results

整体程序花费时间较多,remove处消耗太多时间, 超过24%左右

改良版:

 1 class Solution:
 2     def permute(self, nums: List[int]) -> List[List[int]]:
 3         results = []
 4         len_n = len(nums)
 5         def backtrack(my_nums, use_nums):
 6             if len(my_nums) == len_n:
 7                 results.append(my_nums)
 8                 return
 9             for i in range(len(use_nums)):
10                 x = my_nums.copy()
11                 x.append(use_nums[i])
12                 backtrack(x, use_nums[:i]+use_nums[i+1:])
13         backtrack([], nums)
14         return results

这个每次递归会缩小剩下可用数字范围,不需要维护数字使用dict

my_nums每次都copy一份,这样就不需要remove操作了,运行时间缩短。

超过98%以上Python速度

---最近转战英文leetcode站了,所以题目变成了英文描述

原文地址:https://www.cnblogs.com/watch-fly/p/leetcode_46_watchfly.html

时间: 2024-08-04 03:28:21

leetcode 46 Permutations Python 实现(回溯算法)的相关文章

DFS解法的两道题 Leetcode 46 Permutations & Leetcode 78 Subset

Leetcode 78 Subset Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If S = [1,2,3], a solution is: [ [3], [1

leetCode 46. Permutations 回溯问题 | Medium

46. Permutations(全排列问题--回溯问题经典) Given a collection of distinct 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],   [3,2,1] ] 题目大意:求一个序列的全排列. 思路:做排列

[Lintcode]15. Permutations/[Leetcode]46. Permutations

15. Permutations/46. Permutations 本题难度: Medium Topic: Search & Recursion Description Given a list of numbers, return all possible permutations. Example Example 1: Input: [1] Output: [ [1]] Example 2: Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3]

LeetCode 46 Permutations (全排列)

Given a collection of distinct 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], [3,2,1] ] 题目链接:https://leetcode.com/problems/permutations/ 题目大意:求全排列 题目分析:求

LeetCode 46 Permutations(全排列问题)

题目链接:https://leetcode.com/problems/permutations/?tab=Description Problem:给出一个数组(数组中的元素均不相同),求出这个数组能够产生的所有全排列 采用递归算法,传入参数 List<List<Integer>> list, List<Integer> tempList, int[] nums, boolean[] used 其中list保存最终结果 tempList保存其中一个全排列 nums为最初的

LeetCode 46. Permutations

原题 Given a collection of distinct 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], [3,2,1] ] 解题思路 递归:递归的方法,创建一个visit判断此值是否已经添加过,每一层不断地循环,加入没有被访问的元素,直到最后结果的长

leetcode 46 Permutations ----- java

Given a collection of distinct 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], [3,2,1] ] 给定一个数组,求所有的排列组合. 做出这道题很简单,主要是要比较快的速度,第一次做,耗时5ms,比较慢,主要就是用递归,每次向arr

LeetCode 46 Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 思路,使用字典序法,与http://blog.csdn.net/mlweixiao/article/detail

19.2.7 [LeetCode 46] Permutations

Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] 1 class Solution { 2 public: 3 void build(vector<bool>visited, vector<int>nums,