28 字符串的排列

来源:互联网 发布:mac xlsx文件怎么打开 编辑:程序博客网 时间:2024/06/07 03:11

描述:


实现代码:

package question28;import java.util.ArrayList;import java.util.List;public class StringPermutation {staticArrayList<String> list=new ArrayList<String>();public static void main(String[] args) {// TODO Auto-generated method stub           String str="abc";           char pStr[]=str.toCharArray();            Permutation(pStr);          System.out.println(list);         }private static void Permutation(char[] pStr) {if (pStr==null||pStr.length==0) {return;}Permutation(pStr,0);}private static void Permutation(char[] pStr,int index) {  //index 代表待处理的字符数组的开始位置// TODO Auto-generated method stubif (index==pStr.length-1) {  StringBuffer sb=new StringBuffer();for (char c : pStr) {sb.append(String.valueOf(c));}if (!list.contains(sb.toString())) {  // 防止加入重复的 例如输入字符串"aa" ,只需要输出["aa"],而不是["aa","aa"] list.add(sb.toString());} }else {for (int i=index; i<pStr.length; i++) {// 交换char temp=pStr[index];pStr[index]=pStr[i];pStr[i]=temp;Permutation(pStr, index+1);  // 交换回来temp=pStr[index];pStr[index]=pStr[i];pStr[i]=temp;}}}}

测试结果:


0 0
原创粉丝点击