// 121 算重复 其中2个1 , 123 不算
package com.zl.jvm;
import java.util.ArrayList;
public class
TestSet {
private static boolean check( char[] ch,char
temp) {
for (int z = 0; z <
ch.length; z++) {
if
(ch[z] == temp) {
return false;
}
}
return true;
}
public static void
main(String[] args) {
//先算出2位的不重复的自然数
ArrayList<String> s2 = new ArrayList<>();
char[] a = { ‘0‘, ‘1‘, ‘2‘, ‘3‘,
‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘ };
for (int
i = 0; i < a.length; i++) {
char temp1 = a[i];
if (‘0‘ == temp1) {
continue;
}
for (int j =
0; j < a.length; j++) {
char temp2 = a[j];
if (temp1 ==
temp2) {
continue;
}
s2.add("" + temp1 + temp2);
}
}
//System.out.println(s2);
ArrayList<String> s =
wei(s2,a,10); //10代长度为10位的不重复的 ,s2是先计算出长度是2 位的不重复的自然数,
System.out.println(s.size());
}
public static
ArrayList<String> wei(ArrayList<String>s,char[] a,int count)
{
ArrayList<String> s3 = new ArrayList<>();
if(count>2) {
count-=1;
for
(int i = 0; i < s.size(); i++) {
StringBuilder sb = new
StringBuilder();
String temp = s.get(i);
sb.append(temp);
char[] chars2 = temp.toCharArray();
for (int j = 0; j
< a.length; j++) {
if(check(chars2,a[j]))
{
s3.add(sb.toString() +
a[j]);
}
}
}
return wei(s3,a,count);
}
return
s;
}
}
原文:http://www.cnblogs.com/or2-/p/3627375.html