当前位置:网站首页>LeetCode_ 46_ Full Permutation
LeetCode_ 46_ Full Permutation
2022-07-23 13:51:00 【Fitz1318】
Topic link
Title Description
Give an array without duplicate numbers nums , Back to its All possible permutations . You can In any order Return to the answer .
Example 1:
Input :nums = [1,2,3]
Output :[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Example 2:
Input :nums = [0,1]
Output :[[0,1],[1,0]]
Example 3:
Input :nums = [1]
Output :[[1]]
Tips :
1 <= nums.length <= 6-10 <= nums[i] <= 10numsAll integers in Different from each other
Their thinking
backtracking
Note here that it is necessary to record whether a number has been used , Because the title says nums The numbers inside are different from each other , So it can be used path.contains(nums[i]) To determine whether this number has been used
The retrospective trilogy
- Recursive function parameters
nums: Given setpath: The results of the record meet the conditions ofans: Set of recorded results
- Termination conditions
- Find the leaves , namely
path,size() == nums.length
- Find the leaves , namely
- Single layer logic
- Every secondary investment search , Then skip the used numbers
AC Code
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<Integer> path = new ArrayList<>();
List<List<Integer>> ans = new ArrayList<>();
backTracing(nums, path, ans);
return ans;
}
private static void backTracing(int[] nums, List<Integer> path, List<List<Integer>> ans) {
if (path.size() == nums.length) {
ans.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < nums.length; i++) {
if (path.contains(nums[i])) {
continue;
}
path.add(nums[i]);
backTracing(nums, path, ans);
path.remove(path.size() - 1);
}
}
}
边栏推荐
- CSDN recommended template
- 单例模式实现及防止反射与序列化
- 概率沉思录:2.The quantitative rules
- 【STM32】串口通信基础知识
- 学会用canvas构建折线图、柱状图、饼状图
- Ros2 self study notes: URDF robot modeling
- 基于OpenCV实现对图片及视频中感兴趣区域颜色识别
- [cocos creator] spin animation, monitoring and playback end
- Unity about local loading pictures involves webrequest or byte
- 数据库-视图详探
猜你喜欢

Client does not support authentication protocol requested by server; consider upgrading MySQL client

Hardware system architecture of 4D millimeter wave radar

魔兽地图编辑器触发器笔记

Elephant Swap的LaaS方案优势分析,致eToken表现强势

How to deal with the new development mode when doing testing?

解决MySQL向表中增加数据插入中文乱码问题

Okaleido tiger NFT即将登录Binance NFT平台,你期待吗?

CenterNet目标检测模型及CenterFusion融合目标检测模型

IP地址分类及范围

Événements courants de la souris et du clavier
随机推荐
Smart canteen data analysis system
IP address classification and range
类和对象(上)
MySQL面试题
[图形学]ASTC纹理压缩格式
微信小程序--动态设置导航栏颜色
Tutorial on principles and applications of database system (040) -- MySQL query (II): set the column name or expression to be queried
Remove title block
Convergence of abnormal integral
C#:in、out、ref关键字
LeetCode_51_N皇后
Color recognition of regions of interest in pictures and videos based on OpenCV
IP地址分类及范围
QNX modify system time
[Muduo] poller abstract class
高中语文教资考试大纲
挖财开户风险性大吗,安全吗?
China leads the United States in another emerging technology field and stands at the commanding height of scientific and technological innovation
Special lecture 5 combinatorial mathematics learning experience (long-term update)
第二章关系数据库课后习题