当前位置:网站首页>(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();
}
}
}
边栏推荐
- Buzzer experiment based on stm32f103zet6 library function
- Market status and development prospect forecast of global 4-methyl-2-pentanone industry in 2022
- New Zhongda chongci scientific and Technological Innovation Board: annual revenue of 284million and proposed fund-raising of 557million
- Bit. Store: long bear market, stable stacking products may become the main theme
- Market status and development prospect of resorcinol derivatives for skin products in the world in 2022
- 数仓的字符截取三胞胎:substrb、substr、substring
- 买股票在券商经理的开户链接上开户安全吗?求大神赐教
- 如何利用 RPA 实现自动化获客?
- 循环遍历及函数基础知识
- External interrupt experiment based on stm32f103zet6 library function
猜你喜欢

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

Bit. Store: long bear market, stable stacking products may become the main theme

数据分析师太火?月入3W?用数据告诉你这个行业的真实情况

Exporting coordinates of points in TXT format in ArcGIS

基于STM32F103ZET6库函数按键输入实验

binder hwbinder vndbinder

The IPO of Yuchen Airlines was terminated: Guozheng was proposed to raise 500million yuan as the major shareholder

GIS remote sensing R language learning see here

拥抱云原生:江苏移动订单中心实践

明美新能源冲刺深交所:年应收账款超6亿 拟募资4.5亿
随机推荐
Market status and development prospect forecast of global epoxy resin active toughener industry in 2022
Campus book resource sharing platform
Core dynamic Lianke rushes to the scientific innovation board: with an annual revenue of 170million yuan, Beifang Electronics Institute and Zhongcheng venture capital are shareholders
基于STM32F103ZET6库函数按键输入实验
Informatics Orsay all in one 1335: [example 2-4] connected block
Bit. Store: long bear market, stable stacking products may become the main theme
从感知机到前馈神经网络的数学推导
如何封裝調用一個庫
Mathematical derivation from perceptron to feedforward neural network
Redis 原理 - String
Garbage collector driving everything -- G1
On thread safety
Google Earth Engine(GEE)——ImageCollection (Error)遍历影像集合产生的错误
清华徐勇、段文晖研究组开发出高效精确的第一性原理电子结构深度学习方法与程序
Seven phases of CMS implementation
通过 G1 GC Log 重新认识 G1 垃圾回收器
Market status and development prospect forecast of global 4-methyl-2-pentanone industry in 2022
金源高端IPO被终止:曾拟募资7.5亿 儒杉资产与溧阳产投是股东
驾驭一切的垃圾收集器 -- G1
谈谈线程安全