当前位置:网站首页>8. < tag backtracking and full arrangement > lt.46 Full Permutation + lt.47 Full arrangement II
8. < tag backtracking and full arrangement > lt.46 Full Permutation + lt.47 Full arrangement II
2022-06-09 12:01:00 【Caicai's big data development path】
lt.46. Full Permutation
[ Case needs ]

[ Thought analysis ]
[ Code implementation ]
class Solution {
List<List<Integer>> lists = new ArrayList<>();
public List<List<Integer>> permute(int[] nums) {
// Record the path of leaf nodes list
List<Integer> path = new ArrayList<>();
// The record is to traverse a path , Whether the nodes on this path are used
boolean[] used = new boolean[nums.length];
backTracking(nums, path, used);
return lists;
}
//1. Recursive function , Trace back the combined path of all nodes .
// Parameters : path Is to record each sub path , used Mark whether a node is used
public void backTracking(int[] nums, List<Integer> path, boolean[] used){
//2. Recursion end condition
// path The path recorded in is equal to the length of the selection list
//nums The array is used for choices such as 1,2,3/ path Only when the leaf node is recorded will it be equal to nums The length of
if(path.size() == nums.length){
lists.add(new ArrayList(path)); //?????
return;
}
// The process of backtracking ,
// for Loop breadth traversal
// Backtracking is responsible for traversing a path , Then return to the initial position
for(int i = 0; i < nums.length; i++){
// The current path node is already in use
}
if(used[i] == true)continue;
path.add(nums[i]);
used[i] = true;
backTracking(nums, path, used);
path.remove(path.size() - 1); /// ????
used[i] = false;
}
}
}
lt.47. Full Permutation II
[ Case needs ]

[ Thought analysis ]
[ Code implementation ]
class Solution {
List<List<Integer>> lists = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> permuteUnique(int[] nums) {
// Record the path of leaf nodes list
// The record is to traverse a path , Whether the nodes on this path are used
boolean[] used = new boolean[nums.length];
Arrays.sort(nums);
backTracking(nums, used);
return lists;
}
//1. Recursive function , Trace back the combined path of all nodes .
// Parameters : path Is to record each sub path , used Mark whether a node is used
public void backTracking(int[] nums, boolean[] used){
//2. Recursion end condition
// path The path recorded in is equal to the length of the selection list
//nums The array is used for choices such as 1,2,3/ path Only when the leaf node is recorded will it be equal to nums The length of
if(path.size() == nums.length){
lists.add(new ArrayList(path)); //?????
return;
}
// The process of backtracking ,
// for Loop breadth traversal
// Backtracking is responsible for traversing a path , Then return to the initial position
for(int i = 0; i < nums.length; i++){
if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) {
continue;
}
if(used[i] == false){
path.add(nums[i]);
used[i] = true;
backTracking(nums, used);
path.remove(path.size() - 1); /// ????
used[i] = false;
}
}
}
}
边栏推荐
- 电脑快速索引查询软件Listary
- 8.搜索插入位置
- H3C certified routing switching network senior engineer
- H3C认证云计算高级工程师
- [untitled]
- spark 写入doris太慢方案解决
- Real questions and answers of comprehensive knowledge of system integration project management engineer in the second half of 2021
- 5. bracket generation
- 13.<tag-二叉树和BST基础>lt.450. 删除二叉搜索树中的节点 dbc
- 【数据中台】00丨开篇词丨数据中台,是陷阱?还是金钥匙?
猜你喜欢

3. < tag backtracking, combination and pruning > lt.17 Letter combination of telephone number

08 | middle stage landing step 3: middle stage planning and design

MySQL 乐观锁、悲观锁、多粒度锁
![[untitled]](/img/c1/35479e8bd3832e3b6d9452768c4f13.png)
[untitled]

5.括号生成

6.两两交换链表中的节点

09 | the fourth step: the construction and delivery of the middle office

3dmax如何建模(一)

8. search insertion position

你的微服务敢独立交付么?
随机推荐
【补丁分析】CVE-2016-8610:对导致拒绝服务的“SSL Death Alert”漏洞补丁分析
5.括号生成
H3C certified cloud computing senior engineer
Simple use of GDB
清空表格选中项clearSelection
2021年下半年系统集成项目管理工程师综合知识真题及答案解析
1.<tag-回溯和组合及其剪枝>lt.77.组合 +剪枝 dyh
Analysis of real questions and answers of system integration project management engineer case analysis in the second half of 2021
12. < tag binary tree and BST foundation > lt.701 Insert operation DBC in binary search tree
MySQL 乐观锁、悲观锁、多粒度锁
How much bandwidth is required to view 1080p, 2k and 4K videos smoothly?
7. remove elements
【转载】搞懂G1垃圾收集器
Aiko ai Frontier promotion (6.9)
H3C认证无线互联网络专家
【数据中台】00丨开篇词丨数据中台,是陷阱?还是金钥匙?
PMP项目管理知识体系
OpenSSL security vulnerability (cve-2016-8610) repair steps
Configurationmanager pose flash
13.<tag-二叉树和BST基础>lt.450. 删除二叉搜索树中的节点 dbc