当前位置:网站首页>LeetCode 46:全排列
LeetCode 46:全排列
2022-07-03 10:04:00 【斯沃福德】
思路:回溯
多叉树框架:
回溯框架:
回溯算法就是纯暴力穷举 !
先固定第一位为 1,然后第二位可以是 2,那么第三位只能是 3;然后可以把第二位变成 3,第三位就只能是 2 了;然后就只能变化第一位,变成 2,然后再穷举后两位……其实这就是回溯算法
可以看作是一棵多叉树 !
用path记录遍历的节点,当遍历到叶子节点时将path存入res 就向上返回,
每向上返回一层就将path的末尾删除一个节点,以此维护path;
用onPath布尔数组记录单条线路上的遍历情况避免重复,
class Solution {
List<List<Integer>> res;
public List<List<Integer>> permute(int[] nums) {
int n=nums.length;
boolean[] onPath=new boolean[n];
res=new LinkedList<>();
LinkedList<Integer> path=new LinkedList<>();
dfs(nums,path,onPath);
return res;
}
void dfs(int[] nums,LinkedList<Integer> path, boolean[] onPath){
//终止条件
if(path.size()==nums.length){
res.add(new LinkedList(path));
return;
}
for(int i=0;i<nums.length;i++){
if(onPath[i]){
// 若已遍历过
continue;
}
//递归之前做选择
onPath[i]=true;
path.add(nums[i]);
//进入下一层
dfs(nums,path,onPath);
//递归之后撤销选择
onPath[i]=false;
path.removeLast();
}
}
}
或直接用path.contains(nums[i]) 来判断:
class Solution {
List<List<Integer>> res;
public List<List<Integer>> permute(int[] nums) {
int n=nums.length;
res=new LinkedList<>();
LinkedList<Integer> path=new LinkedList<>();
dfs(nums,path);
return res;
}
void dfs(int[] nums,LinkedList<Integer> path){
//终止条件
if(path.size()==nums.length){
res.add(new LinkedList(path));
return;
}
for(int i=0;i<nums.length;i++){
if(path.contains(nums[i])){
// 若已遍历过
continue;
}
//递归之前做选择
path.add(nums[i]);
//进入下一层
dfs(nums,path);
//递归之后撤销选择
path.removeLast();
}
}
}
边栏推荐
- ExecutorException: Statement returned more than one row, where no more than one was expected.
- Project management essence reading notes (6)
- Qt:qss custom qgroupbox instance
- 帝国cms 无缩略图 灵动标签(e:loop)判断有无标题图片(titlepic)的两种写法
- EPS电动转向系统分析
- Is pinduogai's sales safe in 2022?
- Oracle 11g single machine cold standby database
- Analysis of JMM memory model
- Expandablelistview that can expand and shrink (imitating the list page of professional selection of Zhilian recruitment)
- Qt:qss custom QSlider instance
猜你喜欢
After 8 years of industry thinking, the test director has a deeper understanding of test thinking
Clion debug
Unique in the industry! Fada electronic contract is on the list of 36 krypton hard core technology enterprises
The testing department of the company came to the king of the Post-00 roll, and the veteran exclaimed that it was really dry, but
Hard goods | write all the codes as soon as you change the test steps? Why not try yaml to realize data-driven?
What are the strengths of "testers"?
Encapsulation attempt of network request framework of retro + kotlin + MVVM
反正切熵(Arctangent entropy):2022.7月最新SCI论文
. Net core - a queuing system for wechat official account
The five-year itch of software testing engineers tells the experience of breaking through bottlenecks for two years
随机推荐
Encapsulation attempt of network request framework of retro + kotlin + MVVM
2022-07-02:以下go语言代码输出什么?A:编译错误;B:Panic;C:NaN。 package main import “fmt“ func mai
How can UI automated testing get out of trouble? How to embody the value?
The highest monthly salary of 18K has a good "mentality and choice", and success is poor "seriousness and persistence"~
The element form shows the relationship between elementary transformation and elementary matrix
在腾讯云容器服务Node上执行 kubectl
做软件测试三年,薪资不到20K,今天,我提出了辞职…
面試題總結(2) IO模型,集合,NIO 原理,緩存穿透,擊穿雪崩
EPS电动转向系统分析
POI excel 单元格换行
浅析-JMM内存模型
读书笔记:《心若菩提》 曹德旺
8年测试工程师总结出来的《测试核心价值》与《0基础转行软件测试超全学习指南》
What is the salary level of 17k? Let's take a look at the whole interview process of post-95 Test Engineers
I have been doing software testing for three years, and my salary is less than 20K. Today, I put forward my resignation
Expandablelistview that can expand and shrink (imitating the list page of professional selection of Zhilian recruitment)
Solve the problem that pycharm Chinese input method does not follow
T5 attempt
10. Nacos source code construction
one hot 独热码