12345这五个数,打印出它所有的组合。4不能在中间,3和5不能挨着(实现二)

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;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值