当前位置:网站首页>LeetCode_46_全排列
LeetCode_46_全排列
2022-07-23 07:02:00 【Fitz1318】
题目链接
题目描述
给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
示例 1:
输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
示例 2:
输入:nums = [0,1]
输出:[[0,1],[1,0]]
示例 3:
输入:nums = [1]
输出:[[1]]
提示:
1 <= nums.length <= 6-10 <= nums[i] <= 10nums中的所有整数 互不相同
解题思路
回溯法
这里需要注意的是要记录一个数字是否已经使用过了,因为题目中说了nums里面的数字互不相同,所以可以用path.contains(nums[i])来判断是否使用过了这个数字
回溯三部曲
- 递归函数参数
nums:给定的集合path:记录符合条件的结果ans:记录结果的集合
- 终止条件
- 找到叶子,即
path,size() == nums.length
- 找到叶子,即
- 单层逻辑
- 每次从投搜索,然后对已经使用过的数字进行跳过
AC代码
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);
}
}
}
边栏推荐
- 关于this指针
- 解决MySQL向表中增加数据插入中文乱码问题
- 【可視化調度軟件】上海道寧為SMB組織帶來NETRONIC下載、試用、教程
- Probability meditation: 2. The quantitative rules
- php连接sql server
- Knowledge map: basic concepts
- [Part 2] full analysis of oak-d+turnlebot3 robot project
- 图形管线(一)后处理阶段 alpha测试 模版测试 深度测试 混合
- Vs2019:constexpr function "qcountleadingzerobits" cannot generate constant expressions
- 专题讲座5 组合数学 学习心得(长期更新)
猜你喜欢

Don't be silly to distinguish these kinds of storage volumes of kubernetes

Ti single chip millimeter wave radar code walk (XXV) -- angular dimension (3D) processing flow

Point target simulation of SAR imaging (III) -- Analysis of simulation results

Day 8 notes

Convergence of abnormal integral

Method of entering mathematical formula into mark down document

Remote editing and debugging with vscode

Chapter II relational database after class exercises

Point target simulation of SAR imaging (II) -- matlab simulation

Talking about the CPU type of anroid device and the placement directory of so files
随机推荐
Shell运算符、$((运算式))” 或 “$[运算式]、expr方法、条件判断、test condition、[ condition ]、两个整数之间比较、按照文件权限进行判断、按照文件类型进行判断
在虚拟环境下使用pip时默认使用系统环境的pip该怎么办
【 Visual Dispatching Software】 Shanghai Dow Ning apporte netronic download, Trial, tutoriel pour l'Organisation SMB
数据库系统原理与应用教程(044)—— MySQL 查询(六):使用 LIMIT 选项实现分页查询
Qt Creator .pro文件根据kit添加对应库
[graphics] ASTC texture compression format
Ros2 self study notes: gazebo physical simulation platform
关于#redis#的问题:Redis设置数据持久化之后还是会有丢失数据的风险
数据库系统原理与应用教程(050)—— MySQL 查询(十二):SELECT 命令的执行过程分析
数据库系统原理与应用教程(045)—— MySQL 查询(七):聚合函数
Kotlin - Job 任务/取消
概率沉思錄:2.The quantitative rules
Day 10 notes
Don't be silly to distinguish these kinds of storage volumes of kubernetes
Metaapp development interview questions
ES6——周考题
Remote editing and debugging with vscode
轻重链剖分/树链剖分
[play with FPGA in simple terms to learn 10 ----- simple testbench design]
Light chain dissection / tree chain dissection