当前位置: 首页 > 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.wxhsa.cn/company.asp?id=846

相关文章:

  • Gitee DevOps:中国开发者效率革命的本土化解决方案
  • EAS_单点登录跨数据中心问题
  • 鸿蒙应用开发从入门到实战(二):DevEco Studio工具安装
  • 飞驰云联出席“未来出行国际场景创新峰会” 赋能产业新征程!
  • Canvas 计算文字宽高性能高效,解决了开源项目中的一个棘手问题!
  • fastapi
  • 非线性技术之所以重要,是因为非线性问题不像线性问题那样可以统一求解,其复杂性往往要求我们结合理论、几何、数值、统计、甚至物理直觉进行处理。
  • Oracle主键primary key
  • Kubernetes标签(Label)
  • Gitee DevOps平台深度评测:本土化优势如何赋能企业研发效能提升
  • 【SPIE出版】2025计算机视觉和影像计算国际学术会议(CVIC 2025)
  • 密码学工具包中的Hash函数
  • 跟着院士导师做会议口头汇报PPT!
  • 【分享】内外网文件传输方式:从传统痛点到专业解决方案!
  • c# TargetFramework 应该写 net48 还是 net4.8
  • Docker 安装 Elasticsearch 报错
  • 大疆红外TSDK红外照片转RGB888图片JAVA实现方法
  • MCU联网
  • 算法-A*-01 - jack
  • 代码是上午写的,公司是下午解散的!
  • [antlr] 如何在Linux(Ubuntu)环境中安装配置antlr4.9.2
  • 国内开发者如何选择代码管理平台?Gitee、GitHub与Bitbucket深度对比
  • Spring-Android-即时入门-全-
  • 4. 链表
  • Maven-和-Eclipse-全-
  • Prompt、RAG、微调
  • 飞书对程序员下手了,0 代码生成各类系统!!
  • 测试用例设计检查项
  • Android Kotlin请求权限及权限回调处理
  • 版本发布| IvorySQL 4.6 发布