当前位置:网站首页>Full arrangement (medium difficulty)
Full arrangement (medium difficulty)
2022-07-04 12:41:00 【Nulibiao】
Title Overview ( Medium difficulty )
Topic link :
Click me to enter the link
Ideas and code
Train of thought display
This topic is classic to flash back +DFS( Depth first search traversal )
, Here I will not give my explanation on Backtracking ,leetcode The boss has helped us sum up in place , Just follow his questions , Publicize the following here weiwei bosses
He is a very powerful big man , In addition to his own explanation of the problem , Also opened their own github The website helps you learn algorithms , Here I will post its website below :
Full Permutation weiwei Solution to the problem
weiwei bosses github link
At the same time, as for the full arrangement, if you think it is more difficult to read the solution directly , It is recommended that you can directly look at the following one B standing up The Lord has come to introduce you :
Point me into the B standing
At the same time, about the above is DFS and BFS I also give you a link :
Let me see BFS and DFS
Code example
class Solution {
public List<List<Integer>> permute(int[] nums) {
//len Represents the length of the array
int len = nums.length;
//path It's a double ended queue
Deque<Integer> path = new LinkedList<>();
// Set up our res
List<List<Integer>> res = new ArrayList<>();
// Set what we give nums The initial state of each element in the array is false
boolean[] used = new boolean[len];
if(len == 0) {
return res;
}
dfs(path,used,nums,res,0,len);
return res;
}
/* dfs Introduction of method parameters : path Represents a two terminal queue , Used to store numbers on the path we choose boolean Arrays are used to represent nums The selection and selection of each number in the array , Selected as true, Not for false res Used to store the last returned result Our recursive end condition is the same as ours nums Length of array len And the depth of the binary tree depth It has a lot to do with it */
public void dfs(Deque<Integer> path,boolean[] used,int[] nums,List<List<Integer>> res,int depth,int len) {
if(depth == len) {
// Note that the writing here is actually a simple writing method , Is to instantiate one directly list Set, and then pass in the double ended queue parameters directly
res.add(new ArrayList<>(path));
// Declare our recursive termination condition , When the length of the array passed in is equal to our depth, the recursion ends .
return;
}
for(int i = 0 ; i < len ; i++) {
//!used[i] It means that at this time, it is assumed that this number has not been selected , Its opposite must be true, Then you can enter the following statement
if(!used[i]){
// When selected, add numbers to path in
path.addLast(nums[i]);
// When selected, set the status to true
used[i] = true;
// After selecting the elements above, we start our recursion
dfs(path,used,nums,res,depth+1,len);
// After the recursive operation, we need to withdraw
//1: The first step in the undo operation is to set the state to false
used[i] = false;
//2: The second step in the recall operation is to remove the number from path Remove
path.removeLast();
}
}
}
}
边栏推荐
- C language: the sorting problem of circle number reporting
- Clockwise rotation method of event arrangement -- PHP implementation
- 16.内存使用与分段
- C语言:求100-999是7的倍数的回文数
- Single spa, Qiankun, Friday access practice
- C語言:求100-999是7的倍數的回文數
- Global and Chinese market for naval vessel maintenance 2022-2028: Research Report on technology, participants, trends, market size and share
- [ES6] template string: `string`, a new symbol in es2015
- C language: find the length of string
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
猜你喜欢
Data communication and network: ch13 Ethernet
17.内存分区与分页
Error: Failed to download metadata for repo ‘AppStream‘: Cannot download repomd. XML solution
Flet教程之 02 ElevatedButton高级功能(教程含源码)(教程含源码)
DC-5靶机
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 24
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
Clion configuration of opencv
Entitas learning [iv] other common knowledge points
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14
随机推荐
MySQL advanced (Advanced) SQL statement
Haproxy cluster
Ml and NLP are still developing rapidly in 2021. Deepmind scientists recently summarized 15 bright research directions in the past year. Come and see which direction is suitable for your new pit
[notes] in depth explanation of assets, resources and assetbundles
Netgear switch basic configuration command set
Communication tutorial | overview of the first, second and third generation can bus
17. Memory partition and paging
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
C language: the sorting problem of circle number reporting
Interview question MySQL transaction (TCL) isolation (four characteristics)
Article download address
C language array
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
Flet教程之 按钮控件 ElevatedButton入门(教程含源码)
Talk about "in C language"
The frost peel off the purple dragon scale, and the xiariba people will talk about database SQL optimization and the principle of indexing (primary / secondary / clustered / non clustered)
C語言函數
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 20
Clockwise rotation method of event arrangement -- PHP implementation
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14