当前位置:网站首页>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();
}
}
}
边栏推荐
- Encapsulation attempt of network request framework of retro + kotlin + MVVM
- Tencent micro app to get wechat user information
- QT: QSS custom qtreeview instance
- 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
- 有赞CTO崔玉松:有赞Jarvis核心目标是使产品变得更加聪明和可靠
- File upload and download test point
- Ext file system mechanism principle
- How did I grow up in the past eight years as a test engineer of meituan? I hope technicians can gain something after reading it
- 浅析-JMM内存模型
- 触摸与屏幕自动旋转调试
猜你喜欢
![[proteus simulation] 16 channel water lamp composed of 74hc154 four wire to 12 wire decoder](/img/1f/729594930c7c97d3e731987f4c3645.png)
[proteus simulation] 16 channel water lamp composed of 74hc154 four wire to 12 wire decoder

8年测试工程师总结出来的《测试核心价值》与《0基础转行软件测试超全学习指南》

T5 attempt

Overview of testing theory

After 8 years of industry thinking, the test director has a deeper understanding of test thinking

The element form shows the relationship between elementary transformation and elementary matrix

My understanding of testing (summarized by senior testers)

在职美团测试工程师的这八年,我是如何成长的,愿技术人看完都有收获

可以写进简历的软件测试电商项目,不进来get一下?

The five-year itch of software testing engineers tells the experience of breaking through bottlenecks for two years
随机推荐
Software testing e-commerce projects that can be written into your resume, don't you come in and get it?
如何成为一名高级数字 IC 设计工程师(1-2)Verilog 编码语法篇:Verilog 1995、2001、2005 标准
File upload and download test point
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
封装一个koa分布式锁中间件来解决幂等或重复请求的问题
Using activity to realize a simple inputable dialog box
Internet Socket (非)阻塞write/read n个字节
The normal one inch is 25.4 cm, and the image field is 16 cm
Reading notes: heart like Bodhi, Cao Dewang
The element form shows the relationship between elementary transformation and elementary matrix
程序进程管理工具-go supervisor
A simple method of adding dividing lines in recyclerview
ORACLE 11G 单机冷备数据库
What kind of living condition is a tester with a monthly salary of more than 10000?
First line of code kotlin notes
Hal - General
公司测试部门来了个00后卷王之王,老油条感叹真干不过,但是...
IIS does not take effect after modifying the configuration information
解决undefined reference to `__aeabi_uidivmod‘和undefined reference to `__aeabi_uidiv‘错误
T5 attempt