当前位置:网站首页>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();
}
}
}
边栏推荐
- Software testing redis database
- 封装一个koa分布式锁中间件来解决幂等或重复请求的问题
- 2022 pinduogai 100000 sales tutorial
- 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?
- Execute kubectl on Tencent cloud container service node
- 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
- Static library vs shared library
- T5 attempt
- 线性表顺序表综合应用题P18
- What are the strengths of "testers"?
猜你喜欢
Google Earth engine (GEE) - ghsl global population grid dataset 250 meter resolution
Solve undefined reference to`__ aeabi_ Uidivmod 'and undefined reference to`__ aeabi_ Uidiv 'error
00后抛弃互联网: 毕业不想进大厂,要去搞最潮Web3
我对测试工作的一些认识(资深测试人员总结)
EPS电动转向系统分析
My understanding of testing (summarized by senior testers)
The element form shows the relationship between elementary transformation and elementary matrix
(2) Base
历经一个月,终于拿到金蝶Offer!分享一下四面面经+复习资料
Tencent micro app to get wechat user information
随机推荐
Analysis of JMM memory model
历经一个月,终于拿到金蝶Offer!分享一下四面面经+复习资料
英特尔13代酷睿旗舰曝光,单核5.5GHz
栈,单调栈,队列,单调队列
redis那些事儿
Word line and bit line
Summary of interview questions (2) IO model, set, NiO principle, cache penetration, breakdown avalanche
2021 reading summary (continuously updating)
Clion debug
Do you really need automated testing?
反正切熵(Arctangent entropy):2022.7月最新SCI论文
VPP三层网络互联配置
AMS Series 1 - AMS startup process
触摸与屏幕自动旋转调试
Solve the problem that pycharm Chinese input method does not follow
Internet Socket (非)阻塞write/read n个字节
I, a tester from a large factory, went to a state-owned enterprise with a 50% pay cut. I regret it
8年测试工程师总结出来的《测试核心价值》与《0基础转行软件测试超全学习指南》
[true question of the Blue Bridge Cup trials 44] scratch eliminate the skeleton Legion children programming explanation of the true question of the Blue Bridge Cup trials
The role and necessity of implementing serializable interface