当前位置:网站首页>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
边栏推荐
- The leader / teacher asks to fill in the EXCEL form document. How to edit the word / Excel file on the mobile phone and fill in the Excel / word electronic document?
- English abbreviation of role personal attribute
- 2022.4.22-----leetcode. three hundred and ninety-six
- What is BGP server and what are its advantages?
- Analyze the advantages and disadvantages of tunnel proxy IP.
- . net unit test Part 1: common Net unit test framework?
- What businesses use physical servers?
- NPM -- configure Taobao image
- [hands on learning] network depth v2.1 Sequence model
- How to write the resume of Software Test Engineer so that HR can see it?
猜你喜欢
拨号vps会遇到什么问题?
如何选择一台好的拨号服务器?
Use of j-link RTT
W801 / w800 WiFi socket development (II) - UDP Bluetooth control WiFi connection
The leader / teacher asks to fill in the EXCEL form document. How to edit the word / Excel file on the mobile phone and fill in the Excel / word electronic document?
How to set computer IP?
简洁开源的一款导航网站源码
单片机和4G模块通信总结(EC20)
批处理多个文件合成一个HEX
RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimensio
随机推荐
2018 China Collegiate Programming Contest - Guilin Site J. stone game
[Leetcode每日一题]396. 旋转函数
Campus transfer second-hand market source code
如何“优雅”的测量系统性能
What is a proxy IP pool and how to build it?
J-Link RTT使用
What businesses use physical servers?
如何对代理IP进行分类?
浅析静态代理ip的三大作用。
ESP32蓝牙Bluetooth Controller API介绍
W801 / w800 / w806 unique ID / CPUID / flashid
Makefile文件是什么?
[hands on learning] network depth v2.1 Sequence model
2022 Saison 6 perfect Kid Model IPA national race Leading the Meta - Universe Track
《维C中国》乡村助农暖人心第三站嘉宝果农场
CC2541的仿真器CC Debugger使用教程
用TensorFlow实现线性回归(包括过程中出现的问题及解决方法)
Under the pressure of sales, domestic mobile phones began to reduce prices, but they haven't put down their final face
Esp32 message queue using FreeRTOS
leetcode:27. Remove element [count remove]