当前位置:网站首页>LeetCode刷题系列 -- 46. 全排列
LeetCode刷题系列 -- 46. 全排列
2022-08-11 05:53:00 【在河之洲木水】
给定一个不含重复数字的数组 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] <= 10
nums 中的所有整数 互不相同
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/permutations
思路:
此题可以用回溯法
java代码:
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
fullPermutation(nums, result, new LinkedList<>());
return result;
}
public void fullPermutation(int[] nums, List<List<Integer>> result, List<Integer> subList) {
// 说明本次排列已经将所有元素排完
if (subList.size() == nums.length) {
result.add(new LinkedList<>(subList));
return;
}
for (int num : nums) {
if (subList.contains(num)) {
continue;
}
subList.add(num);
fullPermutation(nums, result, subList);
subList.remove(subList.size() - 1);
}
}
}
边栏推荐
- 快速了解集成学习
- concept noun
- MySQL 版本升级心得
- OA project meeting notice (query & whether attending & feedback for details)
- 拼多多API接口(附上我的可用API)
- Pinduoduo API interface (attach my available API)
- Daily SQL - find each of the students school gpa minimum (window)
- Daily sql: request for friend application pass rate
- Redis测试
- Taobao sku API interface (PHP example)
猜你喜欢
导航定位中的坐标系
Daily sql-statistics of the number of professionals (including the number of professionals is 0)
HCIP-Spanning Tree (802.1D, Standard Spanning Tree/802.1W: RSTP Rapid Spanning Tree/802.1S: MST Multiple Spanning Tree)
mmdetection的安装和训练、测试didi数据集的步骤(含结果)
自定义MVC增删改查
使用Keras构建GAN,以Mnist为例
亚马逊获得AMAZON商品详情 API 返回值说明
OA project meeting notice (query & whether attending & feedback for details)
图的拉普拉斯矩阵
exness:黄金1800关口遇阻,静待美国CPI出炉
随机推荐
maxwell 概念
一个小时快速熟悉MySQL基本用法
radix-4 FFT 原理和C语言代码实现
Concurrent programming in eight-part essay
损失函数——交叉熵
Xshell如何连接虚拟机
【@网络工程师:用好这6款工具,让你的工作效率大翻倍!】
OA Project Pending Meeting & History Meeting & All Meetings
【深度学习】什么是互信息最大化?
淘宝商品详情API接口
JVM学习——3——数据一致性
Daily sql - judgment + aggregation
HCIA knowledge review
李沐d2l(十)--卷积层Ⅰ
抖音API接口
每日sql:求好友申请通过率
HCIP WPN experiment
【预约观看】Ambire 智能钱包 AMA 活动第四期即将举行
HCIP OSPF/MGRE Comprehensive Experiment
OA项目之我的审批(查询&会议签字)