# 0046. 全排列 ## 题目地址(46. 全排列) <https://leetcode-cn.com/problems/permutations/> ## 题目描述 ``` <pre class="calibre18">``` 给定一个 没有重复 数字的序列,返回其所有可能的全排列。 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] ``` ``` ## 前置知识 - 回溯法 ## 公司 - 阿里 - 腾讯 - 百度 - 字节 ## 思路 这道题目是求集合,并不是`求极值`,因此动态规划不是特别切合,因此我们需要考虑别的方法。 这种题目其实有一个通用的解法,就是回溯法。 网上也有大神给出了这种回溯法解题的 [通用写法](https://leetcode.com/problems/combination-sum/discuss/16502/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partitioning)>),这里的所有的解法使用通用方法解答。 除了这道题目还有很多其他题目可以用这种通用解法,具体的题目见后方相关题目部分。 我们先来看下通用解法的解题思路,我画了一张图: ![](https://img.kancloud.cn/46/ec/46ec3382e572355e2109c47284e37e4b_1341x1080.jpg) > 图是 [78.subsets](https://github.com/azl397985856/leetcode/blob/master/problems/78.subsets.md),都差不多,仅做参考。 通用写法的具体代码见下方代码区。 ## 关键点解析 - 回溯法 - backtrack 解题公式 ## 代码 - 语言支持: Javascript, Python3 Javascript Code: ``` <pre class="calibre18">``` <span class="hljs-title">/* * @lc app=leetcode id=46 lang=javascript * * [46] Permutations * * https://leetcode.com/problems/permutations/description/ * * algorithms * Medium (53.60%) * Total Accepted: 344.6K * Total Submissions: 642.9K * Testcase Example: '[1,2,3]' * * 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] * ] * * */</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">backtrack</span>(<span class="hljs-params">list, tempList, nums</span>) </span>{ <span class="hljs-keyword">if</span> (tempList.length === nums.length) <span class="hljs-keyword">return</span> list.push([...tempList]); <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-params">0</span>; i < nums.length; i++) { <span class="hljs-keyword">if</span> (tempList.includes(nums[i])) <span class="hljs-keyword">continue</span>; tempList.push(nums[i]); backtrack(list, tempList, nums); tempList.pop(); } } <span class="hljs-title">/** * @param {number[]} nums * @return {number[][]} */</span> <span class="hljs-keyword">var</span> permute = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">nums</span>) </span>{ <span class="hljs-keyword">const</span> list = []; backtrack(list, [], nums); <span class="hljs-keyword">return</span> list; }; ``` ``` Python3 Code: ``` <pre class="calibre18">``` <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span>:</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">permute</span><span class="hljs-params">(self, nums: List[int])</span> -> List[List[int]]:</span> <span class="hljs-string">"""itertools库内置了这个函数"""</span> <span class="hljs-keyword">return</span> itertools.permutations(nums) <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">permute2</span><span class="hljs-params">(self, nums: List[int])</span> -> List[List[int]]:</span> <span class="hljs-string">"""自己写回溯法"""</span> res = [] <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">_backtrace</span><span class="hljs-params">(nums, pre_list)</span>:</span> <span class="hljs-keyword">if</span> len(nums) <= <span class="hljs-params">0</span>: res.append(pre_list) <span class="hljs-keyword">else</span>: <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> nums: <span class="hljs-title"># 注意copy一份新的调用,否则无法正常循环</span> p_list = pre_list.copy() p_list.append(i) left_nums = nums.copy() left_nums.remove(i) _backtrace(left_nums, p_list) _backtrace(nums, []) <span class="hljs-keyword">return</span> res ``` ``` Python Code: ``` <pre class="calibre18">``` <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span>:</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">permute</span><span class="hljs-params">(self, nums: List[int])</span> -> List[List[int]]:</span> <span class="hljs-string">"""itertools库内置了这个函数"""</span> <span class="hljs-keyword">import</span> itertools <span class="hljs-keyword">return</span> itertools.permutations(nums) <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">permute2</span><span class="hljs-params">(self, nums: List[int])</span> -> List[List[int]]:</span> <span class="hljs-string">"""自己写回溯法"""</span> res = [] <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">_backtrace</span><span class="hljs-params">(nums, pre_list)</span>:</span> <span class="hljs-keyword">if</span> len(nums) <= <span class="hljs-params">0</span>: res.append(pre_list) <span class="hljs-keyword">else</span>: <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> nums: <span class="hljs-title"># 注意copy一份新的调用,否则无法正常循环</span> p_list = pre_list.copy() p_list.append(i) left_nums = nums.copy() left_nums.remove(i) _backtrace(left_nums, p_list) _backtrace(nums, []) <span class="hljs-keyword">return</span> res <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">permute3</span><span class="hljs-params">(self, nums: List[int])</span> -> List[List[int]]:</span> <span class="hljs-string">"""回溯的另一种写法"""</span> res = [] length = len(nums) <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">_backtrack</span><span class="hljs-params">(start=<span class="hljs-params">0</span>)</span>:</span> <span class="hljs-keyword">if</span> start == length: <span class="hljs-title"># nums[:] 返回 nums 的一个副本,指向新的引用,这样后续的操作不会影响已经已知解</span> res.append(nums[:]) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(start, length): nums[start], nums[i] = nums[i], nums[start] _backtrack(start+<span class="hljs-params">1</span>) nums[start], nums[i] = nums[i], nums[start] _backtrack() <span class="hljs-keyword">return</span> res ``` ``` ## 相关题目 - [31.next-permutation](31.next-permutation.html) - [39.combination-sum](39.combination-sum.html) - [40.combination-sum-ii](40.combination-sum-ii.html) - [47.permutations-ii](47.permutations-ii.html) - [60.permutation-sequence](60.permutation-sequence.html) - [78.subsets](78.subsets.html) - [90.subsets-ii](90.subsets-ii.html) - [113.path-sum-ii](113.path-sum-ii.html) - [131.palindrome-partitioning](131.palindrome-partitioning.html)