当前位置:网站首页>Leetcode 46 Full Permutation
Leetcode 46 Full Permutation
2022-06-27 16:58:00 【chenyson】
difficulty : secondary
The frequency of :119
subject :
Give an array without duplicate numbers nums , Back to its All possible permutations . You can In any order Return to the answer .
Their thinking : Backtracking traversal
class Solution {
public List<List<Integer>> permute(int[] nums) {
// The length of the array 、 Number of children
int len=nums.length;
// Create the final result res
List<List<Integer>> res=new ArrayList<>();
// If the array length is 0, Then there is no need to arrange
if(len==0) return res;
// take path Put it in deque in , Because Deque You can take out the front and back
Deque<Integer> path=new ArrayDeque<>(len);
// Of the tag flag, If this number is used , Mark for 1, Not used yet 0
boolean[] flag=new boolean[len];
//DFS Traverse It's actually doing LEN Time DFS Traverse
DFS(nums,len,0,path,flag,res);
return res;
}
public void DFS(int[] nums,int len,int depth,Deque<Integer> path, boolean[] flag
, List<List<Integer>> res){
if(len==depth){
res.add(new ArrayList<>(path));
return ;
}
//len The second traversal is highlighted here || And one. DFS Trees len Three elements are also highlighted here
for(int i=0;i<len;i++){
// If flag yes 0, That is, this number has not been used yet
if(!flag[i]){
// If you haven't used it, use this number as follows
path.addLast(nums[i]);// The root node
flag[i]=true;
DFS(nums,len,depth+1,path,flag,res);
flag[i]=false;
path.removeLast();
}
}
}
}
边栏推荐
- Awk determines whether the first column is greater than 1. If it is greater than 1, the row will be printed
- 郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮
- Special function calculator
- Hongmeng makes efforts! HDD Hangzhou station · offline salon invites you to build ecology
- The interview lasted for half a year. Last month, I successfully got Alibaba p7offer. It was all because I chewed the latest interview questions in 2020!
- 10 minutes to master the installation steps of MySQL
- How to improve it electronic equipment performance management
- Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
- Extract field year / quarter effect based on date
- d3dx9_ How to repair 40.dll? Win10 system d3dx9_ What if 40.dll is lost?
猜你喜欢

Oracle概念三

ROS "topic" programming implementation

LeetCode 124. Binary tree maximum path sum - binary tree series question 8

10分钟掌握mysql的安装步骤

事件监听机制

等保三级密码复杂度是多少?多久更换一次?

Data center table reports realize customized statistics, overtime leave summary record sharing

Array represents a collection of several intervals. Please merge all overlapping intervals and return a non overlapping interval array. The array must exactly cover all the intervals in the input. 【Le

Missing d3d10 How to repair DLL files? Where can I download d3d10.dll

A robot is located in the upper left corner of an M x n grid. The robot can only move down or right one step at a time. The robot attempts to reach the lower right corner of the grid. How many differe
随机推荐
[the way of programmer training] - 3 Character count statistics
C language set operation
d3dx9_ How to repair 40.dll? Win10 system d3dx9_ What if 40.dll is lost?
【多线程】线程通信调度、等待集 wait() 、notify()
事件监听机制
The two trump brand products of Langjiu are resonating in Chengdu, continuously driving the consumption wave of bottled liquor
国家食品安全风险评估中心:不要盲目片面追捧标签为“零添加”“纯天然”食品
关于VS2019C#如何建立登陆界面输入的用户名和密码需与Access数据库的记录相匹配
Detailed explanation of transaction isolation level
How to improve it electronic equipment performance management
Impressive questions
P. Simple application of a.r.a method in Siyuan (friendly testing)
软件测试-测试的概念,单元测试的详细介绍,如何设计测试用例
d3dx9_ How to repair 38.dll? d3dx9_ 38. How to download a missing DLL?
QT5.5.1桌面版安装配置过程中的疑难杂症处理(配置ARM编译套件)
[pyGame games] this "eat everything" game is really wonderful? Eat them all? (with source code for free)
一个机器人位于一个 m x n 网格的左上角 。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角。问总共有多少条不同的路径?【LeetCodeHot100】
Ten common methods of arrays tools
Huawei cloud devcloud launched four new capabilities, setting two domestic firsts
[pygame Games] ce jeu "eat Everything" est fantastique? Tu manges tout? (avec code source gratuit)