62,614
社区成员
发帖
与我相关
我的任务
分享
List<String> result = new ArrayList<>();
@Test
public void test() {
String[] target = new String[]{"a", "b", "c", "d"};
for (int i = 1; i <= target.length; i++)
calc(i, "", target);
System.out.println(Arrays.toString(result.toArray()));
}
//递归
public void calc(int length, String p, String... target) {
for (String item : target)
if (!p.contains(item) && (!((length == 1) && result.add(p + item))))
calc(length - 1, p + item, target);
}
public static void main(String[] args) {
String arr[] = { "a", "b", "c", "d" };
int all = 4;
for (int i = 0; i < 1 << all; i++) {
StringBuffer sb = new StringBuffer();
for (int j = 0; j < all; j++) {
if ((i & (1 << j)) != 0) {
sb.append(arr[j]);
}
}
System.out.println(sb);
}
}