把一个数组里的数组合全部列出,比如1和2列出来为12,21
code
思路就是固定前缀 prefix,然后还有剩下的候选candidate。从候选里面选择一些加到前缀后面。
比如固定前缀1,然后加上2,再从后面的34 选中3,然后是4。得到1234。或者 从后面的34里面先选4,然后是3。得到1243。
static void listAll(List candidate, String prefix) {
if (candidate.isEmpty()) {
System.out.println(prefix);
}
for (int i = 0; i < candidate.size(); i++) {
List temp = new LinkedList(candidate);
listAll(temp, prefix + temp.remove(i));
}
}
代码利用了一些特性,LinkedList 删除操作较快,remove返回的是删除的元素
函数的参数从右边解析,remove 之后,temp 传过去的值也是少了一个的容器。这样candidate渐渐变小,才能达到递归跳出条件。
用 String 实现
static void listAll(String candidate, String prefix) {
if ( candidate.length() == 0) {
System.out.println(prefix);
}
for (int i = 0; i < candidate.length(); i++) {
char prefixChar = candidate.charAt(i);
listAll(candidate.replace(prefixChar + "", ""), prefix + prefixChar);
}
}
测试
public static void main(String[] args) throws IOException {
String[] arrays = new String[] { "1", "2", "3", "4" };
listAll(Arrays.asList(arrays), "");
listAll("1234", "");
}