1,这个是自己写的。一直LTE。
public static ArrayList<String> getPerms(String str) { if (str == null) { return null; } ArrayList<String> permutations = new ArrayList<String>(); if (str.length() == 0) { // base case permutations.add(""); return permutations; } char first = str.charAt(0); // get the first character String remainder = str.substring(1); // remove the first character ArrayList<String> words = getPerms(remainder); for (String word : words) { for (int j = 0; j <= word.length(); j++) { String s = insertCharAt(word, first, j); permutations.add(s); } } return permutations; } public static String insertCharAt(String word, char c, int i) { String start = word.substring(0, i); String end = word.substring(i); return start + c + end; } public static ArrayList<String> getPermutation(String str) { ArrayList<String> permutations = getPerms(str); ArrayList<String> res3 = new ArrayList(); String max = new String(); int flag = 0; int h = permutations.size(); int sum = 0; while(res3.size() != h){ max=permutations.get(0); for(int i = 0; i < permutations.size(); i++){ if(max.compareTo(permutations.get(i))<0){ max = permutations.get(i); flag = i; } } res3.add(new String(max)); permutations.remove(flag); flag = 0; sum = 0; } return res3; }
2,这是别人写的。
下次再看吧。