数组元素全排列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);}}}

相关内容

热门资讯

原创 夏... 夏天湿热重、脾胃易虚寒,这4道汤健脾祛湿、暖胃护胃、清热不伤阳,适合连续两个月常喝,步骤清晰、做法简...
明日四月十六,记得“吃4样,做... 明日农历四月十六,记得“吃4样,做1事”五谷丰登迎福气,老传统别丢! 时光如梭,转眼间来到了农历四月...
今年目标全国销售网点突破200... 5月16日下午6点,贵阳市吾茶白·贵茶潮饮烘焙概念店里排起小队。 “就要这款,上次喝完一直惦记着。”...
原创 淄... 很多人认识淄博只靠烧烤但真正撑起淄博饮食底蕴的从来不是网红热度而是一代代扎根老城的老字号烟火。这些老...
原创 夏... “赤日炎炎似火烧”,这话一到夏天,可算是说到大家心坎里去了。天热起来,不光人没精神,连胃口也跟着变差...