当前位置:网站首页>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);
}
}
}
边栏推荐
猜你喜欢
抖音API接口
Top20 bracket matching
MySQL导入导出&视图&索引&执行计划
Attitude solution - gyroscope + Euler method
HCIP OSPF dynamic routing protocol
HCIP-Spanning Tree (802.1D, Standard Spanning Tree/802.1W: RSTP Rapid Spanning Tree/802.1S: MST Multiple Spanning Tree)
Concurrent programming in eight-part essay
Cobbleland 博览会 基础系列 1
图的拉普拉斯矩阵
获取拼多多商品信息操作详情
随机推荐
Unity3D 学习路线?
mmdetection的安装和训练、测试didi数据集的步骤(含结果)
抖音API接口
[损失函数]——均方差
强烈推荐一款好用的API接口
概念名词解释
1688 product interface
Conference OA Project My Conference
Trill keyword search goods - API
Redis + lua implements distributed interface current limiting implementation scheme
会议OA项目之我的会议
Taobao sku API interface (PHP example)
每日sql-员工奖金过滤和回答率排序第一
Coordinate system in navigation and positioning
那些事情是用Unity开发项目应该一开始规划好的?如何避免后期酿成巨坑?
《Generative Adversarial Networks》
抖音API接口
京东商品详情API调用实例讲解
Xshell如何连接虚拟机
numpy和tensor增加或删除一个维度