当前位置:网站首页>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);
}
}
}
边栏推荐
- Daily sql-statistics of the number of professionals (including the number of professionals is 0)
- ROS 话题通信理论模型
- 淘宝商品详情API接口
- 第一个C函数:如何实现板级初始化?
- ssh服务攻防与加固
- HCIP experiments (pap, chap, HDLC, MGRE, RIP)
- 技能在赛题解析:交换机防环路设置
- HCIP OSPF/MGRE Comprehensive Experiment
- Daily sql-seek the sum of successful investments in 2016
- HCIP MPLS/BGP Comprehensive Experiment
猜你喜欢
随机推荐
Eight-legged text jvm
Xshell如何连接虚拟机
Monte Carlo
淘宝商品详情API接口
实现通用的、高性能排序和快排优化
淘宝sku API 接口(PHP示例)
Conference OA Project My Conference
1688商品详情接口
1688 product interface
详解BLEU的原理和计算
下一代 无线局域网--强健性
软件测试基本流程有哪些?北京专业第三方软件检测机构安利
矩阵分析——Jordan标准形
淘宝API常用接口与获取方式
损失函数——交叉熵
拼多多API接口(附上我的可用API)
buu—Re(5)
radix-4 FFT 原理和C语言代码实现
daily sql - query for managers and elections with at least 5 subordinates
技术分享 | 实战演练接口自动化如何处理 Form 请求?