public class TestNumber {
/**
*
* @param args
*/
public static List list = new ArrayList();
public static void main(String[] args) {
group("", "12345");
System.out.println(list.size());
}
public static void group(String str, String nstr) {
if (str.length() != nstr.length()) {
String rest = getRest(str, nstr);
for (int i = 0; i < rest.length(); i++) {
String temp = str + rest.substring(i, i + 1);
if (temp.indexOf("4") != 2 && temp.indexOf("35") == -1
&& temp.indexOf("53") == -1) {// 过滤显示条件,如果去掉此处的判断,就是列出所有字符集的排列组合
if (temp.length() > 4) {
System.out.println(temp);
if (!list.contains(temp)) {
list.add(temp);
}
}
group(temp, nstr);
}
}
}
}
public static String getRest(String str, String nstr) {
String rest = "";
if (nstr.length() > str.length()) {
rest = nstr;
for (int i = 0; i < str.length(); i++) {
rest = rest.replaceFirst(str.substring(i, i + 1), "");// 注意此处的replaceFirst,而不是replaceAll
}
}
return rest;
}
}
/**
*
* @param args
*/
public static List list = new ArrayList();
public static void main(String[] args) {
group("", "12345");
System.out.println(list.size());
}
public static void group(String str, String nstr) {
if (str.length() != nstr.length()) {
String rest = getRest(str, nstr);
for (int i = 0; i < rest.length(); i++) {
String temp = str + rest.substring(i, i + 1);
if (temp.indexOf("4") != 2 && temp.indexOf("35") == -1
&& temp.indexOf("53") == -1) {// 过滤显示条件,如果去掉此处的判断,就是列出所有字符集的排列组合
if (temp.length() > 4) {
System.out.println(temp);
if (!list.contains(temp)) {
list.add(temp);
}
}
group(temp, nstr);
}
}
}
}
public static String getRest(String str, String nstr) {
String rest = "";
if (nstr.length() > str.length()) {
rest = nstr;
for (int i = 0; i < str.length(); i++) {
rest = rest.replaceFirst(str.substring(i, i + 1), "");// 注意此处的replaceFirst,而不是replaceAll
}
}
return rest;
}
}
本文介绍了一个用于生成特定字符串集合的排列组合的Java程序。该程序通过递归算法生成指定字符串的所有可能组合,并根据预设条件过滤结果。例如,程序会排除包含特定子串或字符的组合。
371

被折叠的 条评论
为什么被折叠?



