https://blog.csdn.net/zjkC050818/article/details/71116168
编写一个方法,确定某字符串的所有排列组合。
给定一个string A和一个int n,代表字符串和其长度,请返回所有该字符串字符的排列,保证字符串长度小于等于11且字符串中字符均为大写英文字符,排列中的字符串按字典序从大到小排序。(不合并重复字符串)
测试样例:
"ABC"
返回:["CBA","CAB","BCA","BAC","ACB","ABC"]
ArrayList<String> list = new ArrayList<String>();
public ArrayList<String> getPermutation(String A) {
char[] chars = A.toCharArray();
getString(chars, 0, A.length()-1);
//Collections.sort()默认从小到大排序,所以需要重写Comparator
Collections.sort(list, new Comparator<String>() {
public int compare(String o1, String o2) {
return o2.compareTo(o1);
}
});
return list;
}
private void getString(char[] chars, int left, int right) {
if (left > right)
return;
//当到达字符数组最后一位时,添加到list中
if (left == right) {
list.add(String.valueOf(chars));
return;
}
for (int i = left; i <= right; i++) {
change(chars, left, i);
getString(chars, left + 1, right);
//因为只有一个char[]数组,所以每一次返回需要把数组change回去
change(chars, left, i);
}
}
private void change(char[] chars, int left, int i) {
char tmp = chars[left];
chars[left] = chars[i];
chars[i] = tmp;
}
https://blog.csdn.net/sinat_24048051/article/details/71506713
https://www.bbsmax.com/A/D854p0w5Eg/
public static ArrayList<String> getPermutation(String A) {
// write code here
ArrayList<StringBuffer> res = new ArrayList();
ArrayList<String> finalRes = new ArrayList();
char[] c = A.toCharArray();
Arrays.sort(c);
StringBuffer start = new StringBuffer();
start.append(c[c.length - 1]);
res.add(start);
System.out.println(res);
for (int i = c.length - 2; i >= 0; --i) {
ArrayList<StringBuffer> temp = new ArrayList();
for (StringBuffer sb : res) {
for (int j = 0; j <= sb.length(); ++j) {
StringBuffer tempSb = new StringBuffer(sb);
tempSb.insert(j, c[i]);
temp.add(tempSb);
}
}
res = new ArrayList(temp);
}
for (int i = 0; i < res.size(); ++i) {
finalRes.add(new String(res.get(i)));
}
Collections.sort(finalRes, new Comparator<String>() {
public int compare(String a, String b) {
for (int i = 0; i < a.length() && i < b.length(); ++i) {
if (a.charAt(i) < b.charAt(i)) {
return 1;
} else if (a.charAt(i) > b.charAt(i)) {
return -1;
}
}
if (a.length() > b.length()) {
return -1;
} else {
return 1;
}
}
});
System.out.println(finalRes);
return finalRes;
}
编写一个方法,确定某字符串的所有排列组合。
给定一个string A和一个int n,代表字符串和其长度,请返回所有该字符串字符的排列,保证字符串长度小于等于11且字符串中字符均为大写英文字符,排列中的字符串按字典序从大到小排序。(不合并重复字符串)
测试样例:
"ABC"
返回:["CBA","CAB","BCA","BAC","ACB","ABC"]
ArrayList<String> list = new ArrayList<String>();
public ArrayList<String> getPermutation(String A) {
char[] chars = A.toCharArray();
getString(chars, 0, A.length()-1);
//Collections.sort()默认从小到大排序,所以需要重写Comparator
Collections.sort(list, new Comparator<String>() {
public int compare(String o1, String o2) {
return o2.compareTo(o1);
}
});
return list;
}
private void getString(char[] chars, int left, int right) {
if (left > right)
return;
//当到达字符数组最后一位时,添加到list中
if (left == right) {
list.add(String.valueOf(chars));
return;
}
for (int i = left; i <= right; i++) {
change(chars, left, i);
getString(chars, left + 1, right);
//因为只有一个char[]数组,所以每一次返回需要把数组change回去
change(chars, left, i);
}
}
private void change(char[] chars, int left, int i) {
char tmp = chars[left];
chars[left] = chars[i];
chars[i] = tmp;
}
https://blog.csdn.net/sinat_24048051/article/details/71506713
https://www.bbsmax.com/A/D854p0w5Eg/
public static ArrayList<String> getPermutation(String A) {
// write code here
ArrayList<StringBuffer> res = new ArrayList();
ArrayList<String> finalRes = new ArrayList();
char[] c = A.toCharArray();
Arrays.sort(c);
StringBuffer start = new StringBuffer();
start.append(c[c.length - 1]);
res.add(start);
System.out.println(res);
for (int i = c.length - 2; i >= 0; --i) {
ArrayList<StringBuffer> temp = new ArrayList();
for (StringBuffer sb : res) {
for (int j = 0; j <= sb.length(); ++j) {
StringBuffer tempSb = new StringBuffer(sb);
tempSb.insert(j, c[i]);
temp.add(tempSb);
}
}
res = new ArrayList(temp);
}
for (int i = 0; i < res.size(); ++i) {
finalRes.add(new String(res.get(i)));
}
Collections.sort(finalRes, new Comparator<String>() {
public int compare(String a, String b) {
for (int i = 0; i < a.length() && i < b.length(); ++i) {
if (a.charAt(i) < b.charAt(i)) {
return 1;
} else if (a.charAt(i) > b.charAt(i)) {
return -1;
}
}
if (a.length() > b.length()) {
return -1;
} else {
return 1;
}
}
});
System.out.println(finalRes);
return finalRes;
}