java实现全排列

xiaoxiao2021-02-27  166

public static void permutation(char[] str, int i) { if (i >= str.length) return; if (i == str.length - 1) { System.out.println(String.valueOf(str)); } else { for (int j = i; j < str.length; j++) { swap(str,i,j); permutation(str, i + 1); swap(str,i,j); } } } public static void swap(char []str,int from ,int to){ char temp =str[from]; str[from]=str[to]; str[to]=temp; } //测试如下 public static void main(String [] args){ permutation("1234".toCharArray(),0); }
转载请注明原文地址: https://www.6miu.com/read-16703.html

最新回复(0)