参考代码:
import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); String s = scanner.next(); List<Character> arrc = new ArrayList<Character>(); for(char x: s.toCharArray()) arrc.add(x); Set<String> all = new HashSet<String>(); arrange(arrc, all, 0); System.out.print(all.size()); } public static void arrange(List<Character> arrc, Set<String> all, int first) { if(first == arrc.size()) all.add(arrc.toString()); for(int i = first; i < arrc.size(); i++) { Collections.swap(arrc, first, i); arrange(arrc, all, first + 1); Collections.swap(arrc, first, i); } } }
0.0分
4 人评分