当前位置:网站首页>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);
}
}
}
边栏推荐
- 数据库-视图详探
- 概率沉思录:2.The quantitative rules
- 关于#redis#的问题:Redis设置数据持久化之后还是会有丢失数据的风险
- [Muduo] poller abstract class
- Ti single chip millimeter wave radar 1642 code walk through (0) - General Outline
- Kotlin - 挂起函数 suspend
- 解决MySQL向表中增加数据插入中文乱码问题
- 去除标题栏
- Point target simulation of SAR imaging (I) -- mathematical model
- 七月到底有多热?通过爬虫爬取当月温度信息,并使用matplotlib绘制温度折线图
猜你喜欢
随机推荐
GLIB-CRITICAL g_file_test:assertion ‘filename != null‘ failed
数据库系统原理与应用教程(050)—— MySQL 查询(十二):SELECT 命令的执行过程分析
图像处理5:膨胀
LeetCode_491_递增子序列
freemarker
Typora 修改表格宽度
去除标题栏
2. Les règles quantitatives
面试官:有了解过ReentrantLock的底层实现吗?说说看
微服务重点
Kotlin - Job 任务/取消
[Muduo] poller abstract class
The fourth operation
These five points should be considered in the production of enterprise science and technology exhibition hall
PHP获取当前时间戳三位毫秒 - 毫秒时间戳
魔兽地图编辑器触发器笔记
Chapter II relational database after class exercises
CSDN推荐模板
建立STM32F103C8T6工程模板和STM32 ST-LINK Utilit烧录hex文件
SparkSQL设计及入门,220722,









