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

相关内容

热门资讯

“全球文旅轻创业计划”在京发布... 2025年11月17日上午,“银发文旅项目发布会暨全球文旅轻创业计划启动仪式”在中国传媒大学成功举办...
城事|办理口岸过百,台湾“首来... 据央视新闻消息,19日,国台办举行例行发布会,大陆持续释放旅游福利,首次来大陆的台胞“首来族”可获得...
北京最正规的十大旅行社|途开心... 友友们,来北京旅游,谁不想沉浸式感受一把地道的老北京生活气息呢?但想玩好,选对旅行社至关重要。今天就...
从“看景”到“尝味” :高德扫... 11月18日,高德扫街榜烟火城市系列发布会·烟火成都(全国首站)活动在成都正式举办。活动中,四川省商...
红酒防伪溯源标签怎么看?教你如... 瓶身上那闪闪发光的防伪溯源标签吸引了她的目光,她刚刚从一个陌生的酒庄购买了这瓶红酒,但内心始终无法摆...