字符串的排列
题目:输入一个字符串,打印出该字符串中字符的所有排列。例如输入字符串abc,则输出由字符a、b、c所能排列出来的所有字符串abc、acb、bac、bca、cab和cba。
1 public void putall(String s){ 2 StringBuffer ss = new StringBuffer(s); 3 int pointer = 0; 4 outputK(ss,pointer); 5 } 6 public void outputK(StringBuffer s, int pointer){ 7 int cur = pointer; 8 if(pointer == s.length()){ 9 System.out.println(s.toString()); 10 } 11 else{ 12 for(int i = pointer; i < s.length(); i ++){ 13 // StringBuffer ss = s; 14 char temp = s.charAt(pointer); 15 s.setCharAt(pointer,s.charAt(i)); 16 s.setCharAt(i,temp); 17 outputK(s,pointer+1); 18 //recover to the string before change 19 s.setCharAt(i, s.charAt(pointer)); 20 s.setCharAt(pointer,temp); 21 22 } 23 } 24 } 25 public void pro10(){ 26 String s = "abc"; 27 putall(s); 28 }
posted on 2013-04-16 03:29 Step-BY-Step 阅读(141) 评论(0) 编辑 收藏 举报