当前位置:网站首页>Leetcode46 Full Permutation
Leetcode46 Full Permutation
2022-04-23 02:01:00 【Xicheng Fengyu building】
LeetCode46- Full Permutation
One 、 Title Description

Two 、 Backtracking tree
There are generally two ways to write the backtracking tree of common arrangement problems , The corresponding diagram is given below :
2.1 be based on swap Backtracking tree
The depth of the tree deepens every layer , Then the branches of each node in this layer will be reduced 1, For example 0 Layer is 3 Branches , In the first 1 layer , Each node has only two branches

2.2 be based on visited Backtracking tree of array
Each layer is still expanded by three nodes , But repeated knots will be pruned , This will also get the backtracking tree of the permutation problem

3、 ... and 、 Code implementation
3.1 be based on swap Backtracking tree code
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
solve(nums, 0, res);
return res;
}
public void solve(int[] nums, int startIndex, List<List<Integer>> res) {
if (startIndex >= nums.length) {
List<Integer> curRes = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
curRes.add(nums[i]);
}
res.add(curRes);
return;
}
for (int i = startIndex; i < nums.length; i++) {
swap(nums, i, startIndex);
solve(nums, startIndex + 1, res);
swap(nums, i, startIndex);
}
}
private void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
3.2 be based on visited Array backtracking tree code
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
boolean[] visited = new boolean[nums.length];
solve(nums, 0, res, new ArrayList<>(), visited);
return res;
}
public void solve(int[] nums, int startIndex, List<List<Integer>> res, List<Integer> curRes, boolean[] visited) {
if (startIndex == nums.length) {
res.add(new ArrayList<>(curRes));
return;
}
for (int i = 0; i < nums.length; i++) {
if (visited[i]) {
continue;
}
curRes.add(nums[i]);
visited[i] = true;
solve(nums, startIndex + 1, res, curRes, visited);
curRes.remove(curRes.size() - 1);
visited[i] = false;
}
}
}
版权声明
本文为[Xicheng Fengyu building]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220844276788.html
边栏推荐
猜你喜欢

Communication summary between MCU and 4G module (EC20)

What is a dial-up server and what is its use?

89 logistic回归用户画像用户响应度预测

Challenges often faced by client project management

Esp32 message queue using FreeRTOS

【动手学深度学习V2】循环神经网络-1.序列模型

J-Link RTT使用

What is BGP server and what are its advantages?

Performance introduction of the first new version of cdr2022

W801 / w800 / w806 unique ID / CPUID / flashid
随机推荐
2018 China Collegiate Programming Contest - Guilin Site J. stone game
Quel est le fichier makefile?
配置iptables实现本地端口转发的方法详解
简洁开源的一款导航网站源码
什么是bgp服务器,有哪些优势?
2022 low voltage electrician examination questions and answers
Find the largest number of two-dimensional arrays
What should I pay attention to when using proxy IP?
What is a boolean type?
When should I write unit tests? What is TDD?
什么是api接口?
App optimization and advanced scoreboard Part 2 [Mui + flask + mongodb]
MySQL active / standby configuration binary log problem
Shardingsphere broadcast table and binding table
Introduction to esp32 Bluetooth controller API
Batch multiple files into one hex
Server 2019 the available memory of the server is half of the actual memory
《维C中国》乡村助农暖人心第三站嘉宝果农场
. net unit test Part 1: common Net unit test framework?
世界读书日 | 技术人不要错过的好书(IT前沿技术)