题目:
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] ]
代码
class Solution {
List<List<Integer>> result = new ArrayList<>();// 存放符合条件结果的集合
LinkedList<Integer> path = new LinkedList<>();// 用来存放符合条件结果
boolean[] used;
public List<List<Integer>> 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;
}
}
}
. . .
相关推荐
热门推荐
Windows系统安装多个nodejs版...
201天前
MySQL数据库,从入门到精通:第十四篇...
196天前
springboot+freemark+...
196天前
花一天时间做一个高质量飞机大战游戏,过万...
196天前
python(matplotlib5)—...
196天前
moment时间插件小技巧(本月、上个月...
196天前
Springboot 使用AOP及全局事...
196天前
前后端分离开发中处理跨域问题汇总
196天前
vue3项目创建步骤,以及项目运行失败的...
196天前
Spring Security 官方de...
129天前
最新推荐
W5500检测网线何时拔出并及时重置插座
199天前
REDIS03_redis.conf配置...
199天前
React-快速搭建开发环境
196天前
数据库保存之Java字符串的压缩与解压缩
196天前
【Python】【Fintech】用Py...
196天前
VC++调用STL算法函数有效提升STL...
196天前
linux 下基本操作指令
196天前
高级排序之分割法(以某数为基准分割)
196天前
idea设置显示maven工具栏
196天前
计算机丢失concrt140.dll文件...
188天前
ads via 小工具