当前位置:网站首页>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();
}
}
}
边栏推荐
- Project management essence reading notes (VII)
- 图解网络:什么是虚拟路由器冗余协议 VRRP?
- Commonly used discrete random distribution
- 搭建ADG后,实例2无法启动 ORA-29760: instance_number parameter not specified
- 软件测试工程师的5年之痒,讲述两年突破瓶颈经验
- AMS Series 1 - AMS startup process
- Comment réaliser des tests automatisés pour les tests logiciels embarqués?
- 【obs】封装obs实现采集的基础流程
- ConstraintLayout跟RelativeLayout嵌套出现的莫名奇妙的问题
- 【obs】obs的ini格式的ConfigFile
猜你喜欢
LeetCode 46:全排列
Activity and fragment lifecycle
嵌入式軟件測試怎麼實現自動化測試?
QT: QSS custom qtreeview instance
The element form shows the relationship between elementary transformation and elementary matrix
(二)进制
面試題總結(2) IO模型,集合,NIO 原理,緩存穿透,擊穿雪崩
The highest monthly salary of 18K has a good "mentality and choice", and success is poor "seriousness and persistence"~
做软件测试三年,薪资不到20K,今天,我提出了辞职…
[proteus simulation] 16 channel water lamp composed of 74hc154 four wire to 12 wire decoder
随机推荐
Execute kubectl on Tencent cloud container service node
Oracle 11g single machine cold standby database
Test what the leader should do
Oracle收回权限 & 创建角色
My understanding of testing (summarized by senior testers)
The role and necessity of implementing serializable interface
我对测试工作的一些认识(资深测试人员总结)
Expandablelistview that can expand and shrink (imitating the list page of professional selection of Zhilian recruitment)
进程与线程
The five-year itch of software testing engineers tells the experience of breaking through bottlenecks for two years
File upload and download test point
redis那些事儿
帝国cms 无缩略图 灵动标签(e:loop)判断有无标题图片(titlepic)的两种写法
CorelDRAW Graphics Suite 2022新版功能详情介绍
Empire CMS no thumbnail smart tag (e:loop) two ways to judge whether there is a titlepic
图解网络:什么是虚拟路由器冗余协议 VRRP?
Google Earth engine (GEE) -- when we use the front and back images to make up for the interpolation effect, what if there is no effect?
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
15 software testing Trends Worthy of attention
Analysis of JMM memory model