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

多变量递归-全排列问题

多变量递归-全排列问题

问题描述:给定一个没有重复数字的序列,返回其所有可能的全排列。

/**
*Given a sequence with no duplicate numbers, return all its possible permutations.
*given an array int arr[] = {1,2,3}
*output 
*[1,2,3]
*[1,3,2]
*[2,1,3]
*[2,3,1]
*[3,1,2]
*[3,2,1]
*/
import java.util.*;public class Permutations {public List<List<Integer>> permute(int[] nums) {List<List<Integer>> res = new ArrayList<>();backtrack(nums, new ArrayList<>(), new boolean[nums.length], res);return res;}private void backtrack(int[] nums, List<Integer> path, boolean[] used, List<List<Integer>> res) {if (path.size() == nums.length) {res.add(new ArrayList<>(path));return;}for (int i = 0; i < nums.length; i++) {if (used[i]) continue;used[i] = true;path.add(nums[i]);backtrack(nums, path, used, res);used[i] = false;path.remove(path.size() - 1);}}public static void main(String[] args){int[] arr = {1,2,3,4};List<List<Integer>> res = permute(arr);for(List<Integer> aList : res){System.out.println(aList.toString());}}
}

 

http://www.rkmt.cn/news/2160.html

相关文章:

  • Gitee DevOps:中国开发者效率革命的本土化解决方案
  • fastapi
  • Oracle主键primary key
  • Kubernetes标签(Label)
  • MCU联网
  • 算法-A*-01 - jack
  • [antlr] 如何在Linux(Ubuntu)环境中安装配置antlr4.9.2
  • 国内开发者如何选择代码管理平台?Gitee、GitHub与Bitbucket深度对比
  • Spring-Android-即时入门-全-
  • 4. 链表
  • 测试用例设计检查项
  • 版本发布| IvorySQL 4.6 发布
  • Avalonia Calendar 日历控件遇到 Flyout 或者切换页面时出现的鼠标按下失效的解决方法
  • Vue 2 + Element UI 技术栈的管理端项目和Git使用教程
  • ubuntu22.04.5系统重启后网络配置消失问题
  • 第十届计算机技术与机械电气工程国际学术论坛(ISCME 2025)暨2025年泰山学术论坛-鲁东大学微纳传感器及系统专题论坛
  • FinRL(2)China_A_share_market_tushare.ipynb
  • 应急响应:某网站被挂非法链接
  • 用惯了VO,什么时候需要DTO?
  • WPF 警惕 StylusPlugIn 的多线程安全问题
  • RAG or 微调
  • 什么是AI CRM(人工智能客户关系管理)
  • 完整教程:WPF WriteableBitmap 高性能双缓冲图片显示方案
  • cache的基本原理
  • 如何用 vxe-table 实现2个树表格可以互相拖拽数据
  • CSP 初赛必背
  • 最新安卓版16音轨简谱编辑器软件使用说明
  • 【URP】Unity超分辨率优化实践
  • 0125_命令模式(Command)
  • 通过 GitHub 仓库下载微信 Mac Windows 历史版本(Rodert 提供)