标签:collect java port array code style title abc rar
import java.util.*; public class Solution { public static ArrayList<String> Permutation(String str) { if(str == null) return null; ArrayList<String> list = new ArrayList<>(); char[] chars = str.toCharArray(); list = allSort(chars, 0, list); Collections.sort(list); return list; } public static ArrayList<String> allSort(char[] chars, int index, ArrayList<String> list){ if(index == chars.length-1){ list.add(String.valueOf(chars)); return list; } for(int i=index; i< chars.length; i++){ if(i != index && chars[i] == chars[index])//当要交换的值相同时 continue; exChangeChars(index, i, chars); //修改第一个与后面的位置 allSort(chars,index+1,list); exChangeChars(index, i, chars);//修改成原来的数组 } return list; } /** * 交换两个字符数组的位置 * @param index1 * @param index2 * @param chars */ public static void exChangeChars(int index1, int index2, char[] chars){ char tmp =chars[index1]; chars[index1] = chars[index2]; chars[index2] = tmp; } }
标签:collect java port array code style title abc rar
原文地址:http://www.cnblogs.com/huangyichun/p/6924948.html