news 2026/4/25 4:45:02

华为OD面试手撕真题 - 全排列 (C++ Python JAVA JS GO)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
华为OD面试手撕真题 - 全排列 (C++ Python JAVA JS GO)

这道题出现的频率非常高,几个小伙伴都反馈抽到这道题。

题目描述

给定一个不含重复数字的数组nums,返回其所有可能的全排列。你可以按任意顺序返回答案。

示例一

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

示例二

输入:nums = [0,1] 输出:[[0,1],[1,0]]

示例三

输入:nums = [1] 输出:[[1]]

提示

  • 1 <= nums.length <= 6
  • -10 <= nums[i] <= 10
  • nums中的所有整数互不相同

题解

力扣原题链接

思路:递归回溯

  1. 总体思路:有n个位置,每个位置尝试放置不同数,从而达到获取所有排列方式。前面的位置选择的数,后面的位置不能在选择。
  2. 通过1的思路进行拆解
    • 要想每个位置尝试放置不同数:实现很简单,使用循环遍历原数组就行,每个数都尝试放入就行。
    • 要想实现前面的位置选择的数,后面的位置不能在选择。,使用一个bool数组,进行去重就行。
  3. 经过1 2 的逻辑分析之后,接下来就是递归回溯的基本套路实现就行。递归的终止条件为所有位置都已填充数

使用下面代码的时间复杂度为O(n * n!)

c++

class Solution { public: void dfs(vector<int>& nums, vector<int>& path, vector<vector<int>>& res, vector<bool>& visited) { int n = nums.size(); // 全部数字已放入 if (path.size() == n) { res.push_back(path); return ; } for (int i = 0; i < n; i++) { // 已被之前位置选择 if (visited[i]) { continue; } // 递归回溯 path.push_back(nums[i]); visited[i] = true; dfs(nums, path, res, visited); visited[i] = false; path.pop_back(); } } vector<vector<int>> permute(vector<int>& nums) { int n = nums.size(); vector<vector<int>> res; vector<bool> visited(n, false); vector<int> path; dfs(nums, path, res, visited); return res; } };

JAVA

import java.util.*; class Solution { // DFS 生成全排列 private void dfs(int[] nums, List<Integer> path, boolean[] visited, List<List<Integer>> res) { int n = nums.length; // 所有数字都已放入路径 if (path.size() == n) { res.add(new ArrayList<>(path)); return; } for (int i = 0; i < n; i++) { // 已被之前位置选择 if (visited[i]) { continue; } visited[i] = true; path.add(nums[i]); dfs(nums, path, visited, res); // 回溯 path.remove(path.size() - 1); visited[i] = false; } } public List<List<Integer>> permute(int[] nums) { int n = nums.length; List<List<Integer>> res = new ArrayList<>(); boolean[] visited = new boolean[n]; List<Integer> path = new ArrayList<>(); dfs(nums, path, visited, res); return res; } }

Python

fromtypingimportListclassSolution:defpermute(self,nums:List[int])->List[List[int]]:res=[]n=len(nums)visited=[False]*n# DFS 生成全排列defdfs(path):# 所有数字都已放入路径iflen(path)==n:res.append(path[:])returnforiinrange(n):# 已被之前位置选择ifvisited[i]:continuevisited[i]=Truepath.append(nums[i])dfs(path)# 回溯path.pop()visited[i]=Falsedfs([])returnres

JavaScript

varpermute=function(nums){constn=nums.length;constres=[];constvisited=newArray(n).fill(false);constpath=[];// DFS 生成全排列functiondfs(){// 所有数字都已放入路径if(path.length===n){res.push([...path]);return;}for(leti=0;i<n;i++){// 已被之前位置选择if(visited[i])continue;visited[i]=true;path.push(nums[i]);dfs();// 回溯path.pop();visited[i]=false;}}dfs();returnres;};

Go

funcpermute(nums[]int)[][]int{n:=len(nums)res:=make([][]int,0)visited:=make([]bool,n)path:=make([]int,0,n)// DFS 生成全排列vardfsfunc()dfs=func(){// 所有数字都已放入路径iflen(path)==n{tmp:=make([]int,n)copy(tmp,path)res=append(res,tmp)return}fori:=0;i<n;i++{// 已被之前位置选择ifvisited[i]{continue}visited[i]=truepath=append(path,nums[i])dfs()// 回溯path=path[:len(path)-1]visited[i]=false}}dfs()returnres}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/23 9:22:56

新闻媒体机构采用GLM-4.6V-Flash-WEB自动生成图片说明文字

新闻媒体机构采用GLM-4.6V-Flash-WEB自动生成图片说明文字 在当今信息爆炸的时代&#xff0c;新闻媒体每天要处理海量的图文内容。一张配图背后&#xff0c;往往意味着编辑几分钟甚至更长时间的手动撰写——描述人物、场景、事件背景&#xff0c;确保语义准确且符合发布规范。…

作者头像 李华
网站建设 2026/4/18 11:01:06

导师推荐!9款AI论文软件测评:继续教育写作全攻略

导师推荐&#xff01;9款AI论文软件测评&#xff1a;继续教育写作全攻略 学术写作工具测评&#xff1a;为何需要一份精准的AI论文软件榜单 在当前继续教育与科研需求日益增长的背景下&#xff0c;AI论文写作工具已成为许多学习者和研究者的得力助手。然而&#xff0c;面对市场上…

作者头像 李华
网站建设 2026/4/18 5:40:45

GitHub镜像网站清华源同步GLM-4.6V-Flash-WEB项目

GitHub镜像网站清华源同步GLM-4.6V-Flash-WEB项目 在今天这个AI应用飞速落地的时代&#xff0c;一个开发者最怕遇到什么&#xff1f;不是模型不会写&#xff0c;而是——下不动。 你辛辛苦苦找到一个看起来完美的多模态视觉语言模型&#xff0c;点开Hugging Face或GitHub链接&a…

作者头像 李华