当前位置:网站首页>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();
}
}
}
边栏推荐
- Stack, monotone stack, queue, monotone queue
- AMS series - application startup process
- The role and necessity of implementing serializable interface
- Qt:qss custom QSlider instance
- Internet Socket (非)阻塞write/read n个字节
- Error installing the specified version of pilot
- 可以写进简历的软件测试电商项目,不进来get一下?
- Overview of testing theory
- Imread change image display size
- Probability theory: application of convolution in calculating moving average
猜你喜欢

Hard goods | write all the codes as soon as you change the test steps? Why not try yaml to realize data-driven?

The five-year itch of software testing engineers tells the experience of breaking through bottlenecks for two years

历经一个月,终于拿到金蝶Offer!分享一下四面面经+复习资料

Tencent micro app to get wechat user information

Overview of testing theory

Solve the problem that pycharm Chinese input method does not follow
![[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](/img/e0/c2b1fbe99939d44201401abf1b5a72.png)
[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

How can UI automated testing get out of trouble? How to embody the value?

做软件测试三年,薪资不到20K,今天,我提出了辞职…

I have been doing software testing for three years, and my salary is less than 20K. Today, I put forward my resignation
随机推荐
What experience is there only one test in the company? Listen to what they say
QT: QSS custom qtoolbar and qtoolbox instances
可以写进简历的软件测试电商项目,不进来get一下?
Hard goods | write all the codes as soon as you change the test steps? Why not try yaml to realize data-driven?
QT: QSS custom qtabwidget and qtabbar instances
I have been doing software testing for three years, and my salary is less than 20K. Today, I put forward my resignation
Summary of interview questions (2) IO model, set, NiO principle, cache penetration, breakdown avalanche
Matlab memory variable management command
如何成为一名高级数字 IC 设计工程师(1-3)Verilog 编码语法篇:Verilog 行为级、寄存器传输级、门级(抽象级别)
Reading notes: heart like Bodhi, Cao Dewang
做软件测试三年,薪资不到20K,今天,我提出了辞职…
2022 pinduogai 100000 sales tutorial
进程与线程
Latest sales volume of pinduoduo
公司测试部门来了个00后卷王之王,老油条感叹真干不过,但是...
Clion debug
First line of code kotlin notes
Qt:qss custom qheaderview instance
The element form shows the relationship between elementary transformation and elementary matrix
Touch and screen automatic rotation debugging