package com.hanzheng.algorithm; import java.util.LinkedList; import java.util.List; public class BackTrack { static List<List<String>> result = new LinkedList<>(); public static void main(String[] args) { String[] arr = new String[]{"a", "b", "c", "d", "e"}; permute(arr); System.out.println(result.size()); for (int i = 0; i < result.size(); i++) { System.out.println(result.get(i)); } } private static void permute(String[] arr) { List<String> track = new LinkedList<>(); backtrack(arr, track); } private static void backtrack(String[] arr, List<String> track) { if (track.size() == arr.length) { result.add(new LinkedList<>(track)); return; } for (int i = 0; i < arr.length; i++) { // 元素不重选的全排列 if (track.contains(arr[i])) { continue; } track.add(arr[i]); backtrack(arr, track); track.remove(track.get(track.size() - 1)); } } }
不重选元素的全排列
于 2022-07-08 01:43:43 首次发布