<>1.问题描述

<>2.解决办法

回溯法:
每一位列出所有可能

第一次swap的作用是构造出新的排列
第二次swap的作用相当于回溯的过程

<>3.代码实现
class Solution { public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> arr =new ArrayList<List<Integer>>(); List<Integer> out =new
ArrayList<Integer>(); for(int i:nums){ out.add(i); } int n= nums.length;
huius(arr,out,n,0); return arr; } public void huius(List<List<Integer>>
arr,List<Integer> out,int n,int first ){ if(first==n){ arr.add(new
ArrayList<Integer>(out)); } for(int i=first;i<n;i++){
Collections.swap(out,first,i); huius(arr,out,n,first+1);
Collections.swap(out,first,i); } } }

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信