[Java] 纯文本查看 复制代码 class Test {
public static void main(String[] args){
int cnt2 = fun01("", "1234");
System.out.println("cnt: "+cnt2);
int cnt3 = fun02("", "1234");
System.out.println("cnt: "+cnt3);
}
public static int fun01(String res, String source){
if ( res.length() >= source.length()) { //保证长度
//System.out.println(res);
return 1;
}
int cnt = 0;
for ( char c : source.toCharArray() ) {
if ( res.contains( c+"" ) ) { //保证不重复
continue;
}
cnt += fun01(res+c, source);
}
return cnt;
}
public static int fun02(String res, String source){
if ( res.length() >= source.length()) { //保证长度
//System.out.println(res);
return 1;
}
int cnt = 0;
for ( char c : source.toCharArray() ) {
if ( res.contains( c+"" ) ) { //保证不重复
continue;
}
if ( "".equals(res) && c == '4' ) { //4不能开头
continue;
}
if ( (res.endsWith("3") && c=='1') || (res.endsWith("1") && c=='3') ) { //1,3不在一起
continue;
}
cnt += fun02(res+c, source);
}
return cnt;
}
}
|