当前位置:网站首页>Leetcode 46: full arrangement
Leetcode 46: full arrangement
2022-07-03 11:17:00 【Swarford】
Ideas : to flash back
Multitree framework :
Backtracking framework :
Backtracking algorithm is pure violence !
First fix the number one as 1, And the second one could be 2, So the third place can only be 3; And then you can turn the second one into 3, The third can only be 2 了 ; And then you can only change the first one , become 2, And then the last two …… In fact, this is the backtracking algorithm
It can be regarded as a multi fork tree !
use path Record the nodes traversed , When traversing to the leaf node path Deposit in res Just go back up ,
Every time you return up one layer, you will path Delete a node at the end of , In order to maintain path;
use onPath Boolean array records the traversal on a single line to avoid repetition ,
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){
// Termination conditions
if(path.size()==nums.length){
res.add(new LinkedList(path));
return;
}
for(int i=0;i<nums.length;i++){
if(onPath[i]){
// If it has been traversed
continue;
}
// Make a choice before recursion
onPath[i]=true;
path.add(nums[i]);
// Go to the next level
dfs(nums,path,onPath);
// Undo selection after recursion
onPath[i]=false;
path.removeLast();
}
}
}
Or directly path.contains(nums[i]) To judge :
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){
// Termination conditions
if(path.size()==nums.length){
res.add(new LinkedList(path));
return;
}
for(int i=0;i<nums.length;i++){
if(path.contains(nums[i])){
// If it has been traversed
continue;
}
// Make a choice before recursion
path.add(nums[i]);
// Go to the next level
dfs(nums,path);
// Undo selection after recursion
path.removeLast();
}
}
}
边栏推荐
- 15 software testing Trends Worthy of attention
- C语言日志库zlog基本使用
- CorelDRAW Graphics Suite 2022新版功能详情介绍
- Probability theory: application of convolution in calculating moving average
- AMS series - application startup process
- What kind of living condition is a tester with a monthly salary of more than 10000?
- Software testing e-commerce projects that can be written into your resume, don't you come in and get it?
- 高精度室内定位技术,在智慧工厂安全管理的应用
- Touch and screen automatic rotation debugging
- Hard goods | write all the codes as soon as you change the test steps? Why not try yaml to realize data-driven?
猜你喜欢
ByteDance layoffs, test engineers were almost destroyed: how terrible is the routine behind the recruitment of large factories?
LeetCode 46:全排列
Solve the problem that pycharm Chinese input method does not follow
Software testing redis database
The normal one inch is 25.4 cm, and the image field is 16 cm
Cause: org. apache. ibatis. builder. Builderexception: error parsing SQL mapper configuration problem analysis
A simple method of adding dividing lines in recyclerview
AIDL
Error installing the specified version of pilot
Expandablelistview that can expand and shrink (imitating the list page of professional selection of Zhilian recruitment)
随机推荐
00后抛弃互联网: 毕业不想进大厂,要去搞最潮Web3
Unique in the industry! Fada electronic contract is on the list of 36 krypton hard core technology enterprises
POI excel 单元格换行
ByteDance layoffs, test engineers were almost destroyed: how terrible is the routine behind the recruitment of large factories?
php服务器 与redis交互大量CLOSE_WAIT分析
Empire CMS no thumbnail smart tag (e:loop) two ways to judge whether there is a titlepic
Analysis of JMM memory model
解决undefined reference to `__aeabi_uidivmod‘和undefined reference to `__aeabi_uidiv‘错误
Tablespace creation management and control file management
ConstraintLayout跟RelativeLayout嵌套出现的莫名奇妙的问题
Illustrated network: what is virtual router redundancy protocol VRRP?
First line of code kotlin notes
2022-07-02: what is the output of the following go language code? A: Compilation error; B:Panic; C:NaN。 package main import “fmt“ func mai
Project management essence reading notes (6)
Solve undefined reference to`__ aeabi_ Uidivmod 'and undefined reference to`__ aeabi_ Uidiv 'error
C语言二维数组
Word line and bit line
Summary of interview questions (2) IO model, set, NiO principle, cache penetration, breakdown avalanche
2021 reading summary (continuously updating)
UI自动化测试如何走出困境?价值又如何体现?