题目:
输入一个字符串,打印出该字符串中字符的所有排列
解答:
1 public class Solution { 2 public static void main(String[] args) { 3 4 //Scanner sc = new Scanner(System.in); 5 //String str = sc.nextLine(); 6 7 myPrint("abc"); 8 } 9 10 private static void myPrint(String str) { 11 if(str == null) { 12 return; 13 } 14 15 char[] chs = str.toCharArray(); 16 myPrint(chs, 0); 17 } 18 19 private static void myPrint(char[] str, int i) { 20 if(i >= str.length) { 21 return; 22 } 23 24 if(i == str.length-1) { 25 System.out.println(String.valueOf(str)); 26 } else { 27 for(int j = i; j < str.length; j++) { 28 char temp = str[j]; 29 str[j] = str[i]; 30 str[i] = temp; 31 32 myPrint(str, i+1); 33 34 temp = str[j]; 35 str[j] = str[i]; 36 str[i] = temp; 37 } 38 } 39 } 40 }