您的位置:首页 > 编程语言 > Java开发

java 数字组合代码 按顺序,每组无重复,形成一个无重复的字符串

2016-04-28 22:08 411 查看
public class test {

static String[] str = { "11", "12", "13", "14", "15" };

static int num = 4;

public static void main(String[] args) {

ArrayList<String[]> result = noteCount(str,4);
for (int i = 0; i < result.size(); i++) {
System.out.println(i+"--"+result.size()+"-"+Arrays.toString(result.get(i)));
}

}

public static ArrayList<String[]> noteCount(String[] source, int n) {
ArrayList<String[]> result = new ArrayList<String[]>();
if (n == 1) {
for (int i = 0; i < source.length; i++) {
result.add(new String[] { source[i] });

}
} else if (source.length == n) {
result.add(source);
} else {
String[] psource = new String[source.length - 1];
for (int i = 0; i < psource.length; i++) {
psource[i] = source[i];
}
result = noteCount(psource, n);
ArrayList<String[]> tmp = noteCount(psource, n - 1);
for (int i = 0; i < tmp.size(); i++) {
String[] rs = new String
;
for (int j = 0; j < n - 1; j++) {
rs[j] = tmp.get(i)[j];
}
rs[n - 1] = source[source.length - 1];
result.add(rs);
}
}
return result;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java