題目:輸入一個字串,打印出該字串中字元的所有排列。例如輸入字串abc,則輸出由字元a、b、c所能排列出來的所有字串abc、acb、bac、bca、cab和cba。
阿新 • • 發佈:2019-02-03
題目:輸入一個字串,打印出該字串中字元的所有排列。例如輸入字串abc,則輸出由字元a、b、c所能排列出來的所有字串abc、acb、bac、bca、cab和cba。
/** * */ package interview; import java.util.Arrays; /** * @author clydelou * */ public class Test { /** * @param args */ public static void p(int[] a, int index) { if (a == null || index < 0) return; if (index == (a.length - 1)) System.out.println(Arrays.toString(a)); else { for (int i = index; i < a.length; i++) { int temp = a[i]; a[i] = a[index]; a[index] = temp; p(a, index + 1); temp = a[i]; a[i] = a[index]; a[index] = temp; } } } public static void main(String[] args) { // TODO Auto-generated method stub int[] a = { 1, 2, 3 }; p(a, 0); } }