当前位置:网站首页>Leetcode46.全排列
Leetcode46.全排列
2022-07-16 13:44:00 【Java全栈研发大联盟】
题目传输地址: https://leetcode.cn/problems/permutations/submissions/

代码如下
//递归解法
public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
if(nums.length==1){
result.add(Arrays.asList(nums[0]));
}
for (int i = 0; i < nums.length; i++) {
int[] restNums = getRestNums(nums, i);
List<List<Integer>> list = permute(restNums);
for (List<Integer> list1 : list) {
List<Integer> list2 = new ArrayList<>(list1);
list2.add(nums[i]);
result.add(list2);
}
}
return result;
}
private static int[] getRestNums(int[] nums, int i) {
final int[] restNums = new int[nums.length - 1];
for (int n = 0; n < i; n++) {
restNums[n] = nums[n];
}
for (int n = i + 1; n < nums.length; n++) {
restNums[n - 1] = nums[n];
}
return restNums;
}
边栏推荐
- 层出不穷的终端设备适配需求下 未来的响应式Web设计长什么样?
- 与机器对话,阿里达摩院挑战新一代人机对话技术
- Basic operation of queue
- 【產品人衛朋】2022年產品人必備的13個設計類網站(1.0版)
- Introduction to replacement technology of SAP ABAP CDs view view
- log4j日志配置
- 找不到 cl.exe 解决办法
- C # basic practice of network application programming and asynchronous programming
- [autosar-dem] - 2.4-app SWC how to read mil lamp status and configure mil lamp
- Nature Microbiology | 枯草芽孢杆菌生物膜促进甜瓜生长并抗病
猜你喜欢

才22岁!这位“00后”博士拟任职985高校!

C#网络应用编程,异步编程基础练习

2022 G3 boiler water treatment examination question bank and simulation examination

北京华联BHG Mall持续发力,BHG DAY引领城市消费新热潮

C#网络应用编程,实验4:线程管理练习
![[PaddleSeg源码阅读] 关于PaddleSeg模型返回的都是list这件小事](/img/a4/bc61237e6557762487783f21602973.png)
[PaddleSeg源码阅读] 关于PaddleSeg模型返回的都是list这件小事

10个第一、2个第二,达摩院NLP团队在SemEval 2022的夺冠之旅

Image xmage de Huawei: Cherchez toutes les images du monde et voyez enfin Bodhisattva

Use plt When the savefig() method saves the drawing, the picture is all white or all black

(pytorch进阶之路五)RNN/LSTM/LSTMP/GRU
随机推荐
树莓派利用OpenCV的图像跟踪、人脸识别等
发论文!AI,机器学习,CV大佬科研项目招生!
【golang】cannot unmarshal xxx “ into Go struct field xxx of type xxx
常见的网路设备和网络参考模型,以及常见的网络层协议及数据通信过程
监听拖拽事件,第一次拖拽得不到上传的文件内容,第二次以后就能正常得到上传的文件内容
HALCON联合C#检测表面缺陷——ROI交互(二)(和图片同步缩放裁剪等功能)
[I2C of Renesas ra6m4 development board reads bmp180 air pressure and temperature]
(斐波那契数列)使用函数输出指定范围内Fibonacc的数(PTA)
才22岁!这位“00后”博士拟任职985高校!
MySQL超详细安装教程 手把手教你安装MySQL到使用MySQL 最简单的MySQL安装方式,这种方式装,卸载也简单
Nature Microbiology | 枯草芽孢杆菌生物膜促进甜瓜生长并抗病
【技术碎片】基于指数扩散二分搜索的重名文件重命名后缀
(Note)七彩虹30系列显卡——《一键超频》按键
使用plt.savefig()方法保存绘图时出现图片全白或全黑的问题
Basic operation of stack
定了,6大领域93个开源任务,阿里开源导师带你参与中科院开源之夏2022
ECCV 2022 | open source for generative knowledge distillation of classification, detection and segmentation
July training (day 16) - queue
达梦数据库表SQL语句
C 语言入门(七)