当前位置: 首页 > news >正文

华为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}
http://www.jsqmd.com/news/200289/

相关文章:

  • HTML的img元素无法显示base64图片的原因分析
  • AI训练图片 视频 数据集素材供应商推荐:卓特视觉企业数据训练专家 - 品牌2026
  • ADB logcat查看GLM-4.6V-Flash-WEB在安卓端运行日志
  • 学霸同款2026 AI论文写作软件TOP8:MBA毕业论文高效神器测评
  • Docker镜像源中科大配置教程助力GLM-4.6V-Flash-WEB国内部署
  • ADB日志抓取分析GLM-4.6V-Flash-WEB在Android运行状态
  • Unity 之 设备性能分级与游戏画质设置与设备自动适配指南
  • 多语言高性能异步消息处理与流式计算实践:Python、Java、Go、C++实战方案
  • git commit规范提交GLM-4.6V-Flash-WEB定制化代码更改
  • GitHub镜像网站镜像GLM-4.6V-Flash-WEB项目提升访问速度
  • MyBatisPlus乐观锁机制保障GLM-4.6V-Flash-WEB并发安全
  • UltraISO注册码最新版盗版警告:转向开源GLM-4.6V-Flash-WEB
  • 2026 十大设计师、美工、运营素材网站推荐,适配多行业的图库合集 - 品牌2026
  • 新闻媒体机构采用GLM-4.6V-Flash-WEB自动生成图片说明文字
  • ComfyUI快捷键大全提升GLM-4.6V-Flash-WEB工作效率
  • Git commit squash合并提交保持GLM-4.6V-Flash-WEB历史清晰
  • 多语言分布式任务调度与性能优化实践:Python、Java、Go、C++高效实战方案
  • 图书馆古籍数字化工程中GLM-4.6V-Flash-WEB的作用探讨
  • 2026年最新稀有金属加工行业观察:10家钽棒/铌棒及相关制品企业实力盘点 - 深度智识库
  • 用python生成3d模型文件
  • 基于GLM-4.6V-Flash-WEB的图像问答系统搭建全流程
  • DISM++驱动导出功能备份GLM-4.6V-Flash-WEB显卡驱动
  • 云计算运维专业前景怎么样?
  • 2.各种环境下Redis的安装
  • CSDN官网广告位投放精准触达GLM-4.6V-Flash-WEB目标用户
  • Plugin ‘vits_native‘ failed to load because module ‘vits_native‘
  • 1.Redis概述
  • 立足招投标数据,洞察火电转型新格局:从“被动应对”到“主动破局”的战略跃迁‌
  • ue ‘vits_native’ 插件加载失败 ue ‘xxx’ 插件加载失败
  • Git commit rebase变基操作整理GLM-4.6V-Flash-WEB提交记录