数组元素全排列Java
admin
2024-02-03 11:15:50
import java.util.*;/*** 数组全排序* @author Green.Gee* @date 2022/11/18 20:33* @email green.gee.lu@gmail.com*/
public class FullMutation {// 数组元素全排列public static void main(String[] args) {Scanner in = new Scanner(System.in);// 示例 1 2 3while (in.hasNextInt()) { // 注意 while 处理多个 caseint a = in.nextInt();int b = in.nextInt();int c = in.nextInt();int[] arr = new int[]{a,b,c};// 递归方法
//            mutation(arr,0,arr.length - 1);// 插入组合方法mutation(arr);}}static void swap(int [] arr,int i,int j){int temp = arr[i];arr[i] = arr[j];arr[j] = temp;}public static void mutation(int [] arr){List>  res = new LinkedList<>();res.add(0,Arrays.asList(arr[0]));for(int i = 1; i < arr.length; i++){List>  temp_res = new LinkedList<>();List next = Arrays.asList(arr[i]);for(List item : res){List p = new LinkedList<>();p.addAll(next);p.addAll(item);temp_res.add(p);List n = new LinkedList<>();n.addAll(item);n.addAll(next);temp_res.add(n);for(int j = 1; j < item.size(); j++){List m = new LinkedList<>();m.addAll(item.subList(0,j));m.addAll(next);m.addAll(item.subList(j,item.size()));temp_res.add(m);}}res = temp_res;}System.err.println(res.toString());}public static void mutation(int [] arr,int start,int end){if(start == end){System.out.println(Arrays.toString(arr));return;}for(int i = start;i <= end;i++){swap(arr,start,i);mutation(arr,start + 1,end);swap(arr,start,i);}}}

相关内容

热门资讯

从哈尔滨到长白山闺蜜5天4晚天... 从哈尔滨到长白山闺蜜5天4晚天池+延吉边境:拍照打卡真实体验 一、出发前的纠结:比老板还难搞的攻略,...
搭上低空经济、AI文旅?两连板... 来源:e公司 桂林旅游(000978)回应市场热点。 9月8日,桂林旅游再度涨停,斩获2连板。当天晚...
八达岭长城国庆怎么去最省心?坐... 八达岭长城是来北京必去的景点之一,尤其是第一次来北京的游客。但国庆期间去八达岭,最大的挑战不是爬长城...
秋日睦邻游园会热闹开锣!居民重... (来源:上观新闻) 近日,殷行街道包头路社区睦邻活动室里,举办了一场秋日睦邻趣味游园会,辖区居民、户...
带父母去长白山怎么玩?3天2晚... 带父母去长白山怎么玩?3天2晚天池攻略,有些坑帮你们踩过了 一句话总结:这次带父母去长白山,我选了黑...