当前位置:网站首页>(LC)46. 全排列
(LC)46. 全排列
2022-06-27 17:52:00 【Autonomy`】
给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
示例 1:
输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
示例 2:
输入:nums = [0,1]
输出:[[0,1],[1,0]]
示例 3:
输入:nums = [1]
输出:[[1]]
提示:
1 <= nums.length <= 6
-10 <= nums[i] <= 10
nums 中的所有整数 互不相同
class Solution {
private List<List<Integer>> res = new LinkedList<>();
public List<List<Integer>> permute(int[] nums) {
LinkedList<Integer> track = new LinkedList<>();
boolean[] used = new boolean[nums.length];
backTrack(nums, track, used);
return res;
}
// 路径:存储在track中
// 选择列表:nums中不存在于track中的那些元素,在used[]中标记为false
// 结束条件:nums中所有的元素存在于track中
public void backTrack(int[] nums, LinkedList<Integer> track, boolean[] used) {
// 结束条件
if (track.size() == nums.length) {
res.add(new LinkedList(track));
return;
}
for (int i = 0; i < nums.length; i++) {
// 排除不合法选择
if (used[i]) {
// nums[i]已经在track中
continue;
}
// 做选择
track.add(nums[i]);
used[i] = true;
// 进入下一层决策树
backTrack(nums, track, used);
// 撤销选择
track.removeLast();
used[i] = false;
}
}
}
优化掉used[]数组
class Solution {
private List<List<Integer>> res = new LinkedList<>();
public List<List<Integer>> permute(int[] nums) {
LinkedList<Integer> track = new LinkedList<>();
backTrack(nums, track);
return res;
}
public void backTrack(int[] nums, LinkedList<Integer> track) {
if (track.size() == nums.length) {
res.add(new LinkedList(track));
return;
}
for (int i = 0; i < nums.length; i++) {
if (track.contains(nums[i])) {
continue;
}
track.add(nums[i]);
backTrack(nums, track);
track.removeLast();
}
}
}
边栏推荐
- 基于STM32F103ZET6库函数外部中断实验
- Minmei new energy rushes to Shenzhen Stock Exchange: the annual accounts receivable exceeds 600million and the proposed fund-raising is 450million
- Add in address of idea official website
- Where to look at high-yield bank financial products?
- Current market situation and development prospect forecast of global 3,3 ', 4,4' - biphenyltetracarboxylic dianhydride industry in 2022
- Code and principle of RANSAC
- 谈谈线程安全
- How to encapsulate and call a library
- OpenSSL client programming: SSL session failure caused by an obscure function
- 使用logrotate对宝塔的网站日志进行自动切割
猜你喜欢

DCC888 :Register Allocation

基于STM32F103ZET6库函数蜂鸣器实验

Bit.Store:熊市漫漫,稳定Staking产品或成主旋律

芯动联科冲刺科创板:年营收1.7亿 北方电子院与中城创投是股东

Keras deep learning practice (12) -- facial feature point detection

金源高端IPO被终止:曾拟募资7.5亿 儒杉资产与溧阳产投是股东

深度学习和神经网络的介绍

破解仓储难题?WMS仓储管理系统解决方案

What is ssr/ssg/isr? How do I host them on AWS?

SQL Server - Window Function - 解决连续N条记录过滤问题
随机推荐
形参的默认值-及return的注意事项-及this的使用-和箭头函数的知识
判断一个变量是数组还是对象?
使用logrotate对宝塔的网站日志进行自动切割
买股票在券商经理的开户链接上开户安全吗?求大神赐教
Current market situation and development prospect forecast of the global ductless heating, ventilation and air conditioning system industry in 2022
openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
Market status and development prospect forecast of phenethyl resorcinol for skin care industry in the world in 2022
GIS remote sensing R language learning see here
数仓的字符截取三胞胎:substrb、substr、substring
Market status and development prospect of resorcinol derivatives for skin products in the world in 2022
Character interception triplets of data warehouse: substrb, substr, substring
实施MES管理系统前,要对哪些问题进行评估
今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
网上期货开户安全么?
教你打印自己的日志 -- 如何自定义 log4j2 各组件
Usage of rxjs mergemap
Jinyuan's high-end IPO was terminated: it was planned to raise 750million Rushan assets and Liyang industrial investment were shareholders
Tupu digital twin intelligent energy integrated management and control platform
华大单片机KEIL添加ST-LINK解决方法
OpenSSL client programming: SSL session failure caused by an obscure function