参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

# 46.全排列 [力扣题目链接](https://leetcode.cn/problems/permutations/) 给定一个 没有重复 数字的序列,返回其所有可能的全排列。 示例: * 输入: [1,2,3] * 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] ## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[组合与排列的区别,回溯算法求解的时候,有何不同?| LeetCode:46.全排列](https://www.bilibili.com/video/BV19v4y1S79W/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 此时我们已经学习了[77.组合问题](https://programmercarl.com/0077.组合.html)、 [131.分割回文串](https://programmercarl.com/0131.分割回文串.html)和[78.子集问题](https://programmercarl.com/0078.子集.html),接下来看一看排列问题。 相信这个排列问题就算是让你用for循环暴力把结果搜索出来,这个暴力也不是很好写。 所以正如我们在[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html)所讲的为什么回溯法是暴力搜索,效率这么低,还要用它? **因为一些问题能暴力搜出来就已经很不错了!** 我以[1,2,3]为例,抽象成树形结构如下: ![46.全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20211027181706.png) ### 回溯三部曲 * 递归函数参数 **首先排列是有序的,也就是说 [1,2] 和 [2,1] 是两个集合,这和之前分析的子集以及组合所不同的地方**。 可以看出元素1在[1,2]中已经使用过了,但是在[2,1]中还要在使用一次1,所以处理排列问题就不用使用startIndex了。 但排列问题需要一个used数组,标记已经选择的元素,如图橘黄色部分所示: ![46.全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20211027181706.png) 代码如下: ```cpp vector> result; vector path; void backtracking (vector& nums, vector& used) ``` * 递归终止条件 ![46.全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209174225145.png) 可以看出叶子节点,就是收割结果的地方。 那么什么时候,算是到达叶子节点呢? 当收集元素的数组path的大小达到和nums数组一样大的时候,说明找到了一个全排列,也表示到达了叶子节点。 代码如下: ```cpp // 此时说明找到了一组 if (path.size() == nums.size()) { result.push_back(path); return; } ``` * 单层搜索的逻辑 这里和[77.组合问题](https://programmercarl.com/0077.组合.html)、[131.切割问题](https://programmercarl.com/0131.分割回文串.html)和[78.子集问题](https://programmercarl.com/0078.子集.html)最大的不同就是for循环里不用startIndex了。 因为排列问题,每次都要从头开始搜索,例如元素1在[1,2]中已经使用过了,但是在[2,1]中还要再使用一次1。 **而used数组,其实就是记录此时path里都有哪些元素使用了,一个排列里一个元素只能使用一次**。 代码如下: ```cpp for (int i = 0; i < nums.size(); i++) { if (used[i] == true) continue; // path里已经收录的元素,直接跳过 used[i] = true; path.push_back(nums[i]); backtracking(nums, used); path.pop_back(); used[i] = false; } ``` 整体C++代码如下: ```CPP class Solution { public: vector> result; vector path; void backtracking (vector& nums, vector& used) { // 此时说明找到了一组 if (path.size() == nums.size()) { result.push_back(path); return; } for (int i = 0; i < nums.size(); i++) { if (used[i] == true) continue; // path里已经收录的元素,直接跳过 used[i] = true; path.push_back(nums[i]); backtracking(nums, used); path.pop_back(); used[i] = false; } } vector> permute(vector& nums) { result.clear(); path.clear(); vector used(nums.size(), false); backtracking(nums, used); return result; } }; ``` * 时间复杂度: O(n!) * 空间复杂度: O(n) ## 总结 大家此时可以感受出排列问题的不同: * 每层都是从0开始搜索而不是startIndex * 需要used数组记录path里都放了哪些元素了 排列问题是回溯算法解决的经典题目,大家可以好好体会体会。 ## 其他语言版本 ### Java ```java class Solution { List> result = new ArrayList<>();// 存放符合条件结果的集合 LinkedList path = new LinkedList<>();// 用来存放符合条件结果 boolean[] used; public List> permute(int[] nums) { if (nums.length == 0){ return result; } used = new boolean[nums.length]; permuteHelper(nums); return result; } private void permuteHelper(int[] nums){ if (path.size() == nums.length){ result.add(new ArrayList<>(path)); return; } for (int i = 0; i < nums.length; i++){ if (used[i]){ continue; } used[i] = true; path.add(nums[i]); permuteHelper(nums); path.removeLast(); used[i] = false; } } } ``` ```java // 解法2:通过判断path中是否存在数字,排除已经选择的数字 class Solution { List> result = new ArrayList<>(); LinkedList path = new LinkedList<>(); public List> permute(int[] nums) { if (nums.length == 0) return result; backtrack(nums, path); return result; } public void backtrack(int[] nums, LinkedList path) { if (path.size() == nums.length) { result.add(new ArrayList<>(path)); } for (int i =0; i < nums.length; i++) { // 如果path中已有,则跳过 if (path.contains(nums[i])) { continue; } path.add(nums[i]); backtrack(nums, path); path.removeLast(); } } } ``` ### Python 回溯 使用used ```python class Solution: def permute(self, nums): result = [] self.backtracking(nums, [], [False] * len(nums), result) return result def backtracking(self, nums, path, used, result): if len(path) == len(nums): result.append(path[:]) return for i in range(len(nums)): if used[i]: continue used[i] = True path.append(nums[i]) self.backtracking(nums, path, used, result) path.pop() used[i] = False ``` ### Go ```Go var ( res [][]int path []int st []bool // state的缩写 ) func permute(nums []int) [][]int { res, path = make([][]int, 0), make([]int, 0, len(nums)) st = make([]bool, len(nums)) dfs(nums, 0) return res } func dfs(nums []int, cur int) { if cur == len(nums) { tmp := make([]int, len(path)) copy(tmp, path) res = append(res, tmp) } for i := 0; i < len(nums); i++ { if !st[i] { path = append(path, nums[i]) st[i] = true dfs(nums, cur + 1) st[i] = false path = path[:len(path)-1] } } } ``` ### Javascript ```js /** * @param {number[]} nums * @return {number[][]} */ var permute = function(nums) { const res = [], path = []; backtracking(nums, nums.length, []); return res; function backtracking(n, k, used) { if(path.length === k) { res.push(Array.from(path)); return; } for (let i = 0; i < k; i++ ) { if(used[i]) continue; path.push(n[i]); used[i] = true; // 同支 backtracking(n, k, used); path.pop(); used[i] = false; } } }; ``` ### TypeScript ```typescript function permute(nums: number[]): number[][] { const resArr: number[][] = []; const helperSet: Set = new Set(); backTracking(nums, []); return resArr; function backTracking(nums: number[], route: number[]): void { if (route.length === nums.length) { resArr.push([...route]); return; } let tempVal: number; for (let i = 0, length = nums.length; i < length; i++) { tempVal = nums[i]; if (!helperSet.has(tempVal)) { route.push(tempVal); helperSet.add(tempVal); backTracking(nums, route); route.pop(); helperSet.delete(tempVal); } } } }; ``` ### Rust ```Rust impl Solution { fn backtracking(result: &mut Vec>, path: &mut Vec, nums: &Vec, used: &mut Vec) { let len = nums.len(); if path.len() == len { result.push(path.clone()); return; } for i in 0..len { if used[i] == true { continue; } used[i] = true; path.push(nums[i]); Self::backtracking(result, path, nums, used); path.pop(); used[i] = false; } } pub fn permute(nums: Vec) -> Vec> { let mut result: Vec> = Vec::new(); let mut path: Vec = Vec::new(); let mut used = vec![false; nums.len()]; Self::backtracking(&mut result, &mut path, &nums, &mut used); result } } ``` ### C ```c int* path; int pathTop; int** ans; int ansTop; //将used中元素都设置为0 void initialize(int* used, int usedLength) { int i; for(i = 0; i < usedLength; i++) { used[i] = 0; } } //将path中元素拷贝到ans中 void copy() { int* tempPath = (int*)malloc(sizeof(int) * pathTop); int i; for(i = 0; i < pathTop; i++) { tempPath[i] = path[i]; } ans[ansTop++] = tempPath; } void backTracking(int* nums, int numsSize, int* used) { //若path中元素个数等于nums元素个数,将nums放入ans中 if(pathTop == numsSize) { copy(); return; } int i; for(i = 0; i < numsSize; i++) { //若当前下标中元素已使用过,则跳过当前元素 if(used[i]) continue; used[i] = 1; path[pathTop++] = nums[i]; backTracking(nums, numsSize, used); //回溯 pathTop--; used[i] = 0; } } int** permute(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){ //初始化辅助变量 path = (int*)malloc(sizeof(int) * numsSize); ans = (int**)malloc(sizeof(int*) * 1000); int* used = (int*)malloc(sizeof(int) * numsSize); //将used数组中元素都置0 initialize(used, numsSize); ansTop = pathTop = 0; backTracking(nums, numsSize, used); //设置path和ans数组的长度 *returnSize = ansTop; *returnColumnSizes = (int*)malloc(sizeof(int) * ansTop); int i; for(i = 0; i < ansTop; i++) { (*returnColumnSizes)[i] = numsSize; } return ans; } ``` ### Swift ```swift func permute(_ nums: [Int]) -> [[Int]] { var result = [[Int]]() var path = [Int]() var used = [Bool](repeating: false, count: nums.count) // 记录path中已包含的元素 func backtracking() { // 结束条件,收集结果 if path.count == nums.count { result.append(path) return } for i in 0 ..< nums.count { if used[i] { continue } // 排除已包含的元素 used[i] = true path.append(nums[i]) backtracking() // 回溯 path.removeLast() used[i] = false } } backtracking() return result } ``` ### Scala ```scala object Solution { import scala.collection.mutable def permute(nums: Array[Int]): List[List[Int]] = { var result = mutable.ListBuffer[List[Int]]() var path = mutable.ListBuffer[Int]() def backtracking(used: Array[Boolean]): Unit = { if (path.size == nums.size) { // 如果path的长度和nums相等,那么可以添加到结果集 result.append(path.toList) return } // 添加循环守卫,只有当当前数字没有用过的情况下才进入回溯 for (i <- nums.indices if used(i) == false) { used(i) = true path.append(nums(i)) backtracking(used) // 回溯 path.remove(path.size - 1) used(i) = false } } backtracking(new Array[Boolean](nums.size)) // 调用方法 result.toList // 最终返回结果集的List形式 } } ```