package zuhe;import java.util.HashSet;
import java.util.Set;public class ZuHe { Set<Integer> host = new HashSet<Integer>();
public void combine(int[] a) { if (null == a || a.length == 0)
return;
// 持有所有数
for (int i : a) {
host.add(i);
}
int[] b = new int[a.length];
getCombination(a, 0, b, 0);
} private void getCombination(int[] a, int begin, int b[], int index) { if (index >= a.length)
return;
for (int i = begin; i < a.length; i++) { b[index] = a[i];
printArray(b, index);
getCombination(a, i + 1, b, index + 1);
}
} private void printArray(int[] b, int index) {
// 建立一个副本
Set<Integer> hostClone = new HashSet<Integer>(host);
for (int i = 0; i < index + 1; i++) {
System.out.print(b[i] + " ");
// 去除host中已被选出的数
hostClone.remove(b[i]);
}
if (!hostClone.isEmpty()) {
System.out.print("-> ");
}
for (int i : hostClone) {
System.out.print(i + " ");
}
System.out.println();
} public static void main(String[] args) { ZuHe robot = new ZuHe();
int[] a = {
1, 2, 3
};
robot.combine(a); }}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货